default value for templates

Jan 20, 2009 at 4:34pm
Hi,
I need to implement a method fun() inside a class classA.
The method fun() should be capable of receiving at most 3 arguments with any data type.
I was following the approach as
class A
{
public:
template<class T1, class T2, class T3>
void fun(T1, T2, T3);
};

template<class T1, class T2, class T3>
void A::fun(T1 t1, T2 t2, T3 t3)
{
}

when the method fun is called from main, it is working only if I'm providing 3 arguments. When I give 2 or 1 arguments it is giving me error as follows.

int main()
{
int i=10;
char c='A';
float f=1.0;
A obj;
obj.fun(i,c,f);// working fine
obj.fun(i,c);// giving an error that fun() does not take 2 arguments
}
can anyone tell me how can we give default value in this case, so that the call for fun() with two arguments can work.
Jan 20, 2009 at 11:23pm
Can you just write a second fun() method that takes only two parameters?

Jan 21, 2009 at 7:07am
It is required not to create overloaded functions.
We need to provide single function that can work for 2 or 3 arguments.
Jan 21, 2009 at 9:08am
I was not aware of that function templates can have default values, thought only class templates could have.
Jan 21, 2009 at 5:01pm
Yes function template cannot have default values. This is what causing an issue here. But, is there any other way of implementation such that a function can receive any data type any default values.
Jan 21, 2009 at 5:09pm
I think you can give the template value but set an argument to be optional
1
2
3
4
5
6
7
template<class T1, class T2, class T3>
    void A::fun(T1 t1, T2 t2, T3 t3=0)//default value
    {
		//...
    }
//...
obj.fun<int,char,char>(i,c);//specify types 
Jan 22, 2009 at 1:57am
The above example would be slightly more correct this way:

1
2
template< class T1, class T2, class T3>
void A::fun( T1 t1, T2 t2, T3 t3 = T3() ) { /* */ }


Bazzy's example makes the requirement that T3 be constructible from int.
This code makes only the requirement that T3 be default constructible.
Jan 22, 2009 at 8:32am
Thanks Bazzy and jsmith.
Well it solves problem in a single file.

But if the templates are in different files then we have linking error as
error LNK2019: unresolved external symbol ....
for the function fun()

If I have header file with class declaration and cpp file for function definition. Then linking error will come from the calling function which is present in some third file.
Jan 23, 2009 at 12:24am
You will need to put the implementation of the template function in the header file.
Jan 23, 2009 at 12:42am
1
2
template< class T1, class T2, class T3>
void A::fun( T1 t1, T2 t2, T3 t3 = T3() ) { /* */ }


that doesn't compile on VS2008

'could not deduce template argument for T3'

when

obj.fun(i,c)

so no, i dont think that solves his problem.
Jan 23, 2009 at 2:36pm
Since you can't have default template values for function templates, you should tell which types you are using:
obj.fun<int,char,char>(i,c);
Jan 29, 2009 at 5:42pm
I'm not sure for the data types.
Hence, the issue is still open with templates.
Topic archived. No new replies allowed.