私は次のようなことを試みています:
struct MyType { };
template <typename T>
struct Test
{
static const MyType * const sm_object;
};
template <>
struct Test<void>
{
static const MyType * const sm_object;
};
template <typename T> const MyType * const Test<T>::sm_object = new MyType();
template <> const MyType * const Test<void>::sm_object = new MyType();
これを2つのファイル(a.cppとb.cpp)に含めます。私はコンパイルして取得しようとします:
error C2998: 'const MyType *Test<void>::sm_object' : cannot be a template definition
私のC++構文は悪いと思いますが、私が間違っていることを考えることはできません。
template<>
複数の変換単位でこれが必要なため、変数定義からを削除できません。これにより、リンクエラーが発生します。
フィールドを基本クラスに入れ、CRTPを使用してタイプごとに新しいインスタンスを作成すると、特殊化が邪魔になりませんが、この「直接」フィールド初期化が機能しないのはなぜですか?構文の一部が欠落している必要があります。
私はVS2003を使用しています:(