Compiling the following code snippet with -Wextra, a sensible warning is issued: ====================== struct B {}; struct A : B { const int i; }; ====================== warn.cc:5: warning: non-static const member 'const int A::i' in class without a constructor However, if A is not a derived class, the warning is not given: ====================== struct A { const int i; }; ====================== This is at least inconsistent. I'd vote for a warning in the second case, too.
Btw, the warning in the second case is missing since gcc 3.3.
: Search converges between 2002-10-02-trunk (#92) and 2002-10-03-trunk (#93). Confirmed.
I think this is correct behavior. Without inheritance, the struct is POD, so different rules apply.
Correct. The class without inheritance doesn't need a constructor since objects of this type can be initialized using a brace-enclosed list. The class with inheritance is not POD, so it can't be initialized that way and needs a constructor. This should demonstrate this: ----------------- struct B {}; struct A1 : B { const int i; }; struct A2 { const int i; }; A1 a1 = { 1 }; // not ok A2 a2 = { 1 }; // ok ----------------- g/x> /home/bangerth/bin/gcc-3.4.4-pre/bin/c++ -W -Wall -ansi -pedantic -c x.cc x.cc:3: warning: non-static const member `const int A1::i' in class without a constructor x.cc:6: error: `a1' must be initialized by constructor, not by `{...}' This is therefore not a bug but correct behavior. W.