iakov.dev

A template static member that GCC won't link

C++ hell

· 1 min read

Nobody can know all of C++. The following code fails to link with GCC, complaining about an undefined variable:

template <typename T>
struct core_type
{
public:
	static int m_var;
};

struct test_type : public core_type<test_type>
{
	int m_another_var;
};

template<> int core_type<test_type>::m_var;

If I change the last line to this, it compiles successfully:

template<> int core_type<test_type>::m_var = 0;

And that is the correct behaviour. ISO/IEC 14882 (14.7.3.15) says that an explicit specialization of a static data member of a template is a definition only when it includes an initializer. There is no syntax to define it with default value.

Visual Studio accepts the first version without any error.