I don't understand how C++11 brace initialization rules work here. Having this code:
struct Position_pod { int x,y,z; }; class Position { public: Position(int x=0, int y=0, int z=0):x(x),y(y),z(z){} int x,y,z; }; struct text_descriptor { int id; Position_pod pos; const int &constNum; }; struct text_descriptor td[3] = { {0, {465,223}, 123}, {1, {465,262}, 123}, }; int main() { return 0; }
Note, that the array is declared to have 3 elements, but only 2 initializers are provided.
However it compiles without error, which sounds strange, as the last array element's reference member will be uninitialized. Indeed, it has NULL value:
(gdb) p td[2].constNum $2 = (const int &) @0x0: <error reading variable>
And now the "magic": I changed Position_pod to Position
struct text_descriptor { int id; Position_pod pos; const int &constNum; };
becomes this:
struct text_descriptor { int id; Position pos; const int &constNum; };
and now it gives the expected error:
error: uninitialized const member ‘text_descriptor::constNum'
My question: Why it compiles in the first case, when it should give an error (as in the second case). The difference is, that Position_pod uses C - style brace initialization and Position uses C++11 - style initialization, which call Position's constructor. But how does this affect the possibility to leave a reference member uninitialized?
(Update) Compiler: gcc (Ubuntu 4.8.2-19ubuntu1) 4.8.2
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With