473,405 Members | 2,373 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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.

Aug 1 '07 #1
2 1696
Áőę» 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>();
Aug 1 '07 #2
On 8 1 , 12 20 , <leomayleo...@gmail.comwrote:
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>();
Aug 1 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: CoolPint | last post by:
Can anyone explain how I can make the following function accept an default arguement for the last parameter, which should be an optional functor? template <typename T, typename FUNCTOR> void...
5
by: Suzanne Vogel | last post by:
Is it possible to store a pointer to a template function? eg, template<class T> fooFunc() {...} fptr = fooFunc; // <-- I couldn't find any info here, not even in the forums:...
10
by: rg | last post by:
Hi all, I was wondering if anyone had dealt with a similar problem. I need to use a template function as the parameter for a particular function (also template function). The program compiles...
3
by: Capstar | last post by:
Hi NG, I am trying to get the attached piece of code to work, but I can't figure out what I'm doing wrong. To me it seems that when I don't pass an argument to x::do_something, it should use the...
2
by: xuatla | last post by:
The following is just a sample code to demostrate my question: ----------- template <typename T> class C { public: friend void f1(double i=2) { std::cout << i; } ; };
4
by: aling | last post by:
What's the rule of default argument of function in C++? I found that the default argument of function could not necessary be a constant value. Is it true? Previously I thought that the default...
6
by: Stuart McGraw | last post by:
I am looking for a VBA "format" or "template" function, that is, a function that takes a format string and a varying number of arguments, and substitutes the argument values into the format string...
7
by: quarup | last post by:
I want to specialize a template function that lives inside a class, but am getting a compile error in VS.net 2003. Here's my code: template <class T> class A { public: template <class U> void...
1
by: wuych | last post by:
I have a question about using iterator in template function //*****code starts here***************************** #include <vector> using std::vector; template<typename Tvoid foo( vector<T& a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.