The reason you are getting those errors is because g++ expects your static variables to be defined somewhere.
There are a couple of different ways to fix this:
Since you are using an integral type, you can change your structures to inherit from integral_constant.
template<typename T>
struct Foo : std::integral_constant<MyEnum, MyEnum::val_1>
{
};
template<>
struct Foo<int> : std::integral_constant<MyEnum, MyEnum::val_2>
{
};
You can also declare the variables constexpr
template<typename T>
struct Foo
{
static constexpr MyEnum value = MyEnum::val_1;
};
template<>
struct Foo<int>
{
static constexpr MyEnum value = MyEnum::val_2;
};
You can define the static variables in your header file.
template<typename T>
struct Foo
{
static const MyEnum value = MyEnum::val_1;
};
template<typename T>
const MyEnum Foo<T>::value;
template<>
struct Foo<int>
{
static const MyEnum value = MyEnum::val_2;
};
// enum.cpp
const MyEnum Foo<int>::value;