2012-01-13 40 views
0
template<int* A,int* B> 
void f() 
{ 
} 
template<int A,int B> 
void f() 
{ 
} 
void main() 
{ 
    f<(int*)1,(int*)2>(); 
} 

我想要我的模板finjing f两个specializtions。但是这个代码没有编译。有什么问题?模板函数的两个特化(int和int *)。编译错误

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 

编译器的Visual C++ 2010

+1

好的,你会得到错误,但_what_错误?请修改您的问题以显示实际错误。 – 2012-01-13 07:07:59

回答

3

您要使用的地址作为模板参数。如果你尝试编译使用gcc或铿锵的代码,你得到的,在GCC

test.cpp:11:13: error: a cast to a type other than an integral or enumeration type cannot  appear in a constant-expression 
test.cpp:11:21: error: a cast to a type other than an integral or enumeration type cannot appear in a constant-expression 

和铿锵:

candidate template ignored: invalid explicitly-specified argument for template parameter 'A' 

这是正确的,根据这样的回答:Casting pointer as template argument: Comeau & MSVC compile, GCC fails

那虽然指针被接受,但它们只应该是指向具有外部链接的命名对象的指针。

相关问题