template<class T>
void fun(T){}
template<>
int fun(int){return 0;}
Consider this example, it is rejected by all implementations. However, I haven't found any persuasive provision in the current standard that specifies this explicit specialization declaration is ill-fomred. If exists, what is the rule?
In addition, the potential relevant rule may be that [temp.deduct.decl#2]
If, for the set of function templates so considered, there is either no match or more than one match after partial ordering has been considered ([temp.func.order]), deduction fails and, in the declaration cases, the program is ill-formed.
I think the meaning of "match" is not sufficiently clear here since "match" didn't clearly define anything.
CodePudding user response:
Your template definition did not match, as void fun(T)
is not T fun(T)
in your specialization.
You simply have to change to:
template<class T>
T fun(T){}
template<>
int fun(int){}
BTW: All this results in a lot of warning because you did not return anything :-)
CodePudding user response:
You were not returning anything in your int specialization. And that is illformed code.
The correct syntax would be this :
#include <iostream>
template<class T>
auto fun(T value)
{
std::cout << value << "\n";
}
template<>
auto fun<int>(int value)
{
auto retval = value 2;
std::cout << retval << "\n";
return retval;
}
int main()
{
fun("hello");
int value = fun(40);
return value;
}