I don't have equipment here to test, but for the specializations I think you need to remove the template parameters you are specializing from the template portion and add a <> including the specialized type names.
Terrible explanation, so here is my untested guess:
type is one of the following types (optionally cv-qualified, the qualifiers are ignored):
std::nullptr_t (since C++11);
integral type;
lvalue reference type (to object or to function);
pointer type (to object or to function);
pointer to member type (to member object or to member function);
enumeration type.
So do you just need to make it a template type parameter ? I haven't tested this:
1 2 3 4
template <int n, typename T = double>
T sum(T a){
return a;
}
Edit: It doesn't force the type to double, but you could do a static_assert with a type trait to see that it is.
Also, if you look in the first post, I'm not trying to specialize the non-type parameter(int n), but the type parameter(class T)
Yes, but you had this:
template <double,int n>
The double is being interpreted as a non type parameter, but it must be one of the types that I had shown above. Otherwise a type parameter which is what my small example did.
It's strange that the int specialization compiles without problems.
This is because an integral type can be a non type parameter.
function template has already been defined
Do you have both of these?
1 2 3 4 5 6 7 8 9
template <double,int n>
double sum(double a){
return a;
}
template <int n, typename T = double>
T sum(T a){
return a;
}
It's okay to overload function templates. Overload resolution considers all base templates equally and so it works as you would naturally expect from your experience with normal C++ function overloading: Whatever templates are visible are considered for overload resolution, and the compiler simply picks the best match.
It's a lot less intuitive to specialize function templates. For one thing, you can't partially specialize them -- pretty much just because the language says you can't. For another thing, function template specializations don't overload. This means that any specializations you write will not affect which template gets used, which runs counter to what most people would intuitively expect. After all, if you had written a nontemplate function with the identical signature instead of a function template specialization, the nontemplate function would always be selected because it's always considered to be a better match than a template.
If you're writing a function template, prefer to write it as a single function template that should never be specialized or overloaded, and implement the function template entirely in terms of a class template. This is the proverbial level of indirection that steers you well clear of the limitations and dark corners of function templates. This way, programmers using your template will be able to partially specialize and explicitly specialize the class template to their heart's content without affecting the expected operation of the function template. This avoids both the limitation that function templates can't be partially specialized, and the sometimes surprising effect that function template specializations don't overload. Problem solved.
If you're using someone else's plain old function template (one that's not implemented in terms of a class template as suggested above), and you want to write your own special-case version that should participate in overloading, don't make it a specialization; just make it an overloaded function with the same signature.
In your OP you had 3 specialisations, the first one could evaluate to the same as what you were trying to achieve with the second one.
Does this work?
1 2 3 4 5 6 7 8 9
template <int n, typename T = double>
T sum(T a){
return a;
}
template <int,int n>
int sum(int a){
return a;
}
Also you aren't using the n parameter, is there a need to have it?
When I read up about that C2995 error, it talks a lot about circular includes, lack of include guards and template code in cpp files, hopefully you aren't doing any of those things?
TIM, that code works. I know both specializations in the first post do the same thing, and int n is also not used. This code is just for testing the syntax.