The below code couldn’t pass the compilation, what’s the consideration for this compiler error?
template<class T> void f(T t) {};
template<> void f<char>(char c = 'a') {}
Error message: Default arguments are not allowed on an explicit specialization of a function template
I think that the rationale behind this error is due to the fact that the default arguments in the function template apply to its specialization as well and you are not allowed to define the default argument more than once in C++.
Consider the following:
This will print
ameaning that specialization is called with the default argument defined in the main template.If you need a different default argument for each specialization you can use the approach illustrated below: