template<int* A,int* B>
void f()
{
}
template<int A,int B>
void f()
{
}
void main()
{
f<(int*)1,(int*)2>();
}
I’d like have two specializtions of my template finction f. But this code isn’t compiled. What’s the problem?
Error 1 error C2440: "specialization" : cannot convert from "int *" to "const int" line 11
Error 2 error C2973: invalid template argument "int *" line 11
Error 3 error C2440: "specialization" : cannot convert from "int *" to "const int" line 11
Error 4 error C2973: invalid template argument "int *" line 11
Error 5 error C2668: 'f' : ambiguous call to overloaded function line 11
Compiler Visual C++ 2010
You are trying to use an address as template parameter. If you try to compile the code with gcc or clang, you get, in gcc
and in clang:
which is correct, according to this answer: Casting pointer as template argument: Comeau & MSVC compile, GCC fails
That is, although pointers are accepted, they should only be pointers to named objects with external linkage.