2012-01-13 41 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

那雖然指針被接受,但它們只應該是指向具有外部鏈接的命名對象的指針。

相關問題