Consider the 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 that # 2 is f<int>(const int) instead of f<const int>(const int) . What's going on here? My first thought was that the upper level const discarded in the type conversion of the function, so that type # 2 is void(int) , which leads to the specialization f<int>(const int) . But I'm not so sure about that.
And why does C ++ allow such syntax? I mean, since we cannot partially specialize function templates, we would know the value of the template argument if we want to explicitly highlight it. So why does C ++ not just force the programmer to explicitly provide the values โโof the template arguments when specializing the template functions? (i.e. we would have to write specialization No. 1 in template <> void f<int>(const int) { } or template <> void f<int const>(const int) { } ) Does it have special use, other than coding convenience?
source share