Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

template partial specialization of static fields initialisation

I'm attempting 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 attempt to compile and get:

error C2998: 'const MyType *Test<void>::sm_object' : cannot be a template definition

I assume my C++ syntax is bad, but I can't think what I'm doing wrong.

I can't remove the template<> from the variable definition, as I need this in multiple translation units, and that would result in a link error.

I could put the field into a base class and use the CRTP to create a new instance per type, and then the specialization wouldn't get in the way, but why doesn't this 'direct' field initialisation work? I must be missing some piece of syntax.

I'm using VS2003 :(

like image 553
Tom Whittock Avatar asked Feb 26 '23 11:02

Tom Whittock


1 Answers

Judging from g++ I think you need to remove the template<> from that line and put the remainder in just one source file (not in the header). Since it's a specialization it's just like a normal non-template static which you don't define in a header.

In some .C file:

const MyType * const Test<void>::sm_object = new MyType();

like image 169
Mark B Avatar answered Feb 27 '23 23:02

Mark B