I am trying to do something like the following:
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();
I include this in 2 files - a.cpp and b.cpp. I am trying to compile and get:
error C2998: 'const MyType *Test<void>::sm_object' : cannot be a template definition
I assume that my C ++ syntax is bad, but I can't think what I'm doing wrong.
I canβt remove template<> from the variable definition, since I need it in several translation units, and this will lead to a link error.
I can put the field in the base class and use CRTP to create a new instance for each type, and then specialization will not interfere, but why does this βdirectβ field initialization not work? I need to skip some piece of syntax.
I am using VS2003 :(
source share