Re: Template static member initialization
ALiX wrote:
My template class has a const static data member, which is used by the
constructor. It seems that the program does not produce the correct
result. Either it is the fault of the compiler (gcc 4.1) or I need to
learn more about initialization of static members... The code below
captures the problem and is as simple as I could make it:
#include <iostream>
#include <limits>
template<typename T> struct A {
const static unsigned NONE;
unsigned x;
A() { x = NONE; }
};
template<typename T> const unsigned A<T>::NONE =
numeric_limits<unsigned>::max();
std::numeric_limits
A<double> a;
int main(void) {
Drop the 'void' -- bad style
cout << a.x << "\n";
cout << A<double>::NONE << "\n";
return 0;
}
The output of the program is:
0
4294967295
Mustn't NONE be initialized before a is constructed?
It's not *instantiated* unless it's "used". Perhaps referring to
it in the c-tor does not get it instantiated (and therefore does
not get it initialised), but I would consider it a compiler bug.
Try initialising it right in the class:
template<typename T> struct A {
static unsigned const NONE
= std::numeric_limits<unsigned>::max();
unsigned x;
A() : x(NONE) {}
};
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask