Using default argument in the template function.

  • Thread starter =?gb2312?B?wfXquw==?=
  • Start date
?

=?gb2312?B?wfXquw==?=

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.
 
B

Barry

Áõê» said:
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>();
 
N

nevergone

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>();
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,778
Messages
2,569,605
Members
45,237
Latest member
AvivMNS

Latest Threads

Top