0

こんにちは、テンプレート化されたクラスのサブグループ用に定義したいテンプレート化されたクラスの静的メンバーがあります。

template <typename T> 
class FooT
{
private:
 static int ms_id;
};

template <typename T> 
class Foo {};

template<> template<typename T> int FooT< template Foo<T> >::ms_id = 10;

悲しいことに、gcc 4.1.1 では次のエラーがスローされます。

D:\X\Foo.h(98) : エラー: テンプレート引数 1 が無効です

行で:template<> template<typename T> int FooT< template Foo<T> >::ms_id = 10;

私が間違っているのは、そもそも許可されている一般的な概念ですか?

4

3 に答える 3

3

これは、「初期化テンプレート」を部分的に特殊化することで実行できます。

template <typename T> 
class FooT
{
private:
 static int ms_id;
};

template <typename T> 
class Foo {};

template <typename T>
class GetValue {
  static const int v = 0;
};

template <typename T>
class GetValue< Foo<T> > {
  static const int v = 10;
};

template<typename T> int FooT< T >::ms_id = GetValue<T>::v;
于 2009-10-16T11:46:12.403 に答える
2

確かに、テンプレート クラスをテンプレート インスタンス化の補助として配置することはできません。「具体的な」クラスを配置する必要があります。

たとえば、int の場合:

template <>
int FooT< template Foo< int > >::ms_id = 10; 

また

template<>
int FooT< MyClass >::ms_id = 10; 
于 2009-10-16T11:30:23.667 に答える