Const static members in a template class?

Hi there,
I have a little problem with template classes and their specialization. Here is a short example:


1
2
3
4
5
6
7
8
9
10
11
12
13
template <typename T>
struct A{
// some typedefs

const static T my_constant;
};

template<>
struct A<double>{
//some typedefs

const static double my_constant = 4.23;
};



The above example is not compiling, because of the assignment of the const static double. Double needs a constructor, but that doesn't work (or seems not to work) with static.

I'm not sure, if it works at all in C++ that way. All I want is a template struct with some typedefs and a constant which is different for different specializations. Don't think it has to be static, but that would be better style, wouldn't it?

Thanks for helping.
ElCattivo
If your compiler supports the new feature constexpr of the C++ 2011 Standard then try to change

const static double my_constant = 4.23;

to

constexpr static double my_constant = 4.23;

1
2
3
4
5
6
7
8
template < typename T > struct S { static T sval ; } ;

template < typename T > T S<T>::sval = T() ;


template <> struct S<double> { static double sval ; } ;

double S<double>::sval = 45.678 ;
ok, I will try these two things. Thanks :)
Topic archived. No new replies allowed.