2012-10-23 7 views
11

私は専門のクラスにメソッドを定義することができますどのように...cppにテンプレートの特殊化を定義しますか?

//ヘッダ

template<typename T> 
void func(T){} 

template<> 
void func<int>(int); 

// CPP

template<> 
void func<int>(int) 
{} 

のようなCPPに特化した機能を定義することができますcppで?これと同じように...(動作しない、私はerror C2910: 'A<int>::func' : cannot be explicitly specialized取得)

//ヘッダ

template<typename T> 
struct A 
{ 
    static void func(T){} 
}; 

template<> 
struct A<int> 
{ 
    static void func(int); 
}; 

// CPP

template<> 
void A<int>::func(int) 
{} 
+0

あなたがチェックアウトしましたこの:http://stackoverflow.com/questions/115703/storing-c-template-function-definitions-in-a-cpp-file – Reunanen

+0

@Pukkuこのメソッドの懸念の専門でスタンドアロン関数の特殊化と実際に同じであるテンプレート化されていないクラス(上記の私の動作例のように)。私はcpp内の特殊な_class_でメソッドを定義することを尋ねています。 – David

答えて

4

使用あなたの.cppファイルに構文次

void A<int>::func(int) 
{ 
} 

これはVisual C++の種類の機能です。

は詳細についてはMSDN C2910 error descriptionを参照してください:

このエラーはまた、Visual Studio .NET 2003年に行われたコンパイラ準拠作業の結果として生成されます:.コードがVisual Studio .NET 2003およびVisual Studio .NETバージョンのVisual C++で有効な場合、テンプレート<>を削除します。

+3

これはVisual Studioのコンパイラ依存機能です。質問http://stackoverflow.com/questions/3749099/why-should-the-implementation-and-the-declaration-of-a-template-class-be-in-theおよびhttp://stackoverflow.com/を参照してください。質問/ 495021 /なぜテンプレートがヘッダファイルにのみ実装されるのか – 8bitwide

関連する問題