# Default arguments in function template specializations

This topic is 4716 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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 Realized that the example was stupid, modified it a bit

##### Share on other sites
take out the "=0" on the integer specialization. Sorry, I don't think you can specialize the default parameters, so you have to use your template's base specialization. (could be wrong though)

##### Share on other sites
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 versionFunc(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 on other sites
Quote:
 Original post by greldikSo 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 on other sites
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 on other sites
Quote:
 Original post by greldikIf 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 versionFunc(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 on other sites
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! =)

• 10
• 14
• 11
• 10
• 11