Consider the code below in a single translation unit:
class C {
private:
struct Init {
Init() {
/* compute data once here */
}
};
static const Init& i;
static int data[];
public:
/* interface for reading data */
};
const C::Init& C::i = Init();
int C::data[200];
- Is C::i always initialized after C::data no matter the order of the definition of both?
- Is this solution the most elegant one for computing static data once?
int C::data[200]is zero-initialized, which means that it is statically initialized. Static initialization comes before dynamic initialization. SinceC::Init::Init()is not a constant expression,C::iis dynamically initialized, necessarily afterC::data.See 3.6.2 for details.
A bootleg quote: