Sign in to follow this  
greldik

Default arguments in function template specializations

Recommended Posts

greldik    163
Hi Does anyone know if there is a way around this error in VC6?
template< typename T > void Func(T Arg, int Def = 0);
template<> void Func(int Arg, int Def = 0); // C2572
error C2572: 'Func' : redefinition of default parameter : parameter 2 [edit]Realized that the example was stupid, modified it a bit

Share this post


Link to post
Share on other sites
greldik    163
So you mean that this error occurs on every compiler? I thought it was VC-specific...

If I remove the default value on the specialization I would have to write code like this to actually call the correct version:

float a; int b;
Func(a);
// Func(b); <-- This will still call the default version
Func(b, 0); // <-- This is needed to call specialization

And that's confusing. Of course I could remove the default argument everywhere, but is's nice to have sometimes...=)

Share this post


Link to post
Share on other sites
Drew_Benton    1861
Quote:
Original post by greldik
So you mean that this error occurs on every compiler? I thought it was VC-specific...


In VS7 I get this:
Quote:
f:\Program Files\Microsoft Visual Studio\MYPROJECTS\GLFW Idea Demo 2\Demo.cpp(15): error C2765: 'Func' : an explicit specialization of a function template cannot have any default arguments


In Dev-CPP I get this:
Quote:
13 C:\Documents and Settings\Drew Benton\Desktop\main.cpp default argument specified in explicit specialization


So it's just illegal to do [smile]

Quote:
And that's confusing. Of course I could remove the default argument everywhere, but is's nice to have sometimes...=)


If you wanted to, you could make a macro [wink]

Share this post


Link to post
Share on other sites
JimPrice    815
Second time I've posted this, so here goes...

I think what you're trying to achieve can be solved using overloading (because although you can't partially specialise non-member template functions, you can overload):

template < typename T > void Func(T Arg, int Def);
template < typename T > void Func(T Arg);

Jim.

Edit : just seen you've specialised on int. Can you do the following:

template < typename T > void Func(T Arg, int Def);
template < typename T > void Func(T Arg);
template <> void Func < int > (int Arg);

Share this post


Link to post
Share on other sites
snk_kid    1312
Quote:
Original post by greldik
If I remove the default value on the specialization I would have to write code like this to actually call the correct version:

float a; int b;
Func(a);
// Func(b); <-- This will still call the default version
Func(b, 0); // <-- This is needed to call specialization



No you don't, you just can't change the value of default arguments in specializations, unless VC++ 6.0 is also non-standard compliant in this aspect aswell you should be able to call the specialized version and still use default arguments that are declared in the primary template.

Share this post


Link to post
Share on other sites
greldik    163
Thanks for the replies

Quote:
I think what you're trying to achieve can be solved using overloading

Thanks, I will solve it like that. A lot of overloading though, because I've got 3 default arguments in the actual code. =)

Quote:
unless VC++ 6.0 is also non-standard compliant in this aspect aswell

Actually, trying to do Func(b) generates this error in VC6:
"error C2660: 'Func' : function does not take 1 parameters"
So it actually tries to call the specialized version but can't, because its second argument is not (cannot be) default.

Templates are a mess! =)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this