Static member initialization in a class template
This will work
template <typename T>
struct S
{
static double something_relevant;
};
template<typename T>
double S<T>::something_relevant=1.5;
Since C++17, you can now declare the static member to be inline
, which will define the variable in the class definition:
template <typename T>
struct S
{
...
static inline double something_relevant = 1.5;
};
live: https://godbolt.org/g/bgSw1u
Just define it in the header:
template <typename T>
struct S
{
static double something_relevant;
};
template <typename T>
double S<T>::something_relevant = 1.5;
Since it is part of a template, as with all templates the compiler will make sure it's only defined once.