Consider following case:
#include <iostream>
template <class T> void f(T) { std::cout << "#1\n"; } // #1
template <> void f(const int) { std::cout << "#2\n"; } // #2
int main() {
f<const int>(1); // call #1
f<int>(1); // call #2
return 0;
}
It seems #2 is f<int>(const int)
instead of f<const int>(const int)
. What happens here? My first thought was top level const
is discarded in function type transformation, so that the type of #2 is void(int)
, which results in a specialization of f<int>(const int)
. But I'm not sure about that.
And why C++ allows such syntax? I mean since we can't partial specialize function templates, we would have known the template argument value if we want to explicitly specialize one. So why C++ don't just force programmer explicitly provides template argument values when specializing template functions? (i.e. we would have to write specialization of #1 in template <> void f<int>(const int) { }
or template <> void f<int const>(const int) { }
) Does it have special usage except for coding convenience?