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

私はテンプレートfinuction fの2つの特殊化が必要です。しかし、このコードはコンパイルされていません。どうしたの?テンプレート関数(intとint *)の2つの特殊化。コンパイルエラー

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_というエラーはありますか?実際のエラーを表示するには、質問を編集してください。 –

答えて

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

ことポインタは受け入れられますが、外部リンケージを持つ名前付きオブジェクトへのポインタでなければなりません。