Using default argument in the template function. - c++
This is a discussion on Using default argument in the template function. - c++ ; Hi folks,
I am trying to add a default argument to a template function in a
template class, here is the code snippet:
template<typename T>
class Test {
template<typename U>
friend void doSomething(U level);
};
template<typename U>
void doSomething(U level ...
-
Using default argument in the template function.
Hi folks,
I am trying to add a default argument to a template function in a
template class, here is the code snippet:
template<typename T>
class Test {
template<typename U>
friend void doSomething(U level);
};
template<typename U>
void doSomething(U level = 0) {
cout << "Here I am doing something\n";
}
int main() {
doSomething();
}
An error araise at the compile time:
error C2783: 'void doSomething(U)' : could not deduce template
argument for 'U'
How can I define such a template function taking a default argument?
Thank you.
-
Re: Using default argument in the template function.
Αυκ» wrote:
> Hi folks,
>
> I am trying to add a default argument to a template function in a
> template class, here is the code snippet:
>
> template<typename T>
> class Test {
> template<typename U>
> friend void doSomething(U level);
> };
>
> template<typename U>
> void doSomething(U level = 0) {
> cout << "Here I am doing something\n";
> }
>
> int main() {
> doSomething();
> }
>
> An error araise at the compile time:
>
> error C2783: 'void doSomething(U)' : could not deduce template
> argument for 'U'
>
> How can I define such a template function taking a default argument?
> Thank you.
>
The actual argument passed into the function is used to instantiate the
function template, so if you really want to do in the default argument
way, I think you have explicitly write the template param, like the
following:
doSomething<your type>();
-
Re: Using default argument in the template function.
On 8 1 , 12 20 , <leomayleo...> wrote:
> Hi folks,
>
> I am trying to add a default argument to a template function in a
> template class, here is the code snippet:
>
> template<typename T>
> class Test {
> template<typename U>
> friend void doSomething(U level);
>
> };
>
> template<typename U>
> void doSomething(U level = 0) {
> cout << "Here I am doing something\n";
>
> }
>
> int main() {
> doSomething();
>
> }
dosomething<yourtype>();
Similar Threads
-
By Application Development in forum c++
Replies: 1
Last Post: 11-01-2007, 10:01 AM
-
By Application Development in forum c++
Replies: 2
Last Post: 06-22-2007, 03:25 PM
-
By Application Development in forum c++
Replies: 8
Last Post: 06-21-2007, 01:56 PM
-
By Application Development in forum c++
Replies: 3
Last Post: 06-14-2007, 05:50 AM
-
By Application Development in forum c++
Replies: 3
Last Post: 01-26-2007, 12:00 PM