The following C++ code should not compile: class C { C() { } struct s { const int i; }; }; The C++ standard clause 12.6.2 ; 4 says that for each non-static data member of a POD class type containing a member of a const-qualified type, there must be a member initialiser, or else the program is ill-formed.
As you've written it, class C doesn't have any non-static members. Struct C::s hasn't been declared as a member object of C. const int i is a member of C::s, not C, so C() without member initializers should be acceptable. Note, if you write just : struct s { const int i; }; // This, however, doesn't give any diagnostics until an instance of s is declared. s foo; struct.cc:7: error: structure 'foo' with uninitialized const members Now, I'd need to lookup the standard to determine whether or not a diagnostic is appropriate...
(In reply to comment #1) > As you've written it, class C doesn't have any non-static members. Struct C::s > hasn't been declared as a member object of C. const int i is a member of C::s, > not C, so C() without member initializers should be acceptable. How about this example: struct S { const int i; }; class C { public: C() { } S s; }; void f() { C c; S s; } This fails at the line `S s;' in f(), but the `C c;' line is accepted silently. The standard says the requirement applies to data-members *containing* a member of const-qualified type.
Confirmed with the testcase from attachment #2 [details].
mine ...
patch here: http://gcc.gnu.org/ml/gcc-patches/2010-04/msg01269.html
Subject: Bug 29043 Author: jason Date: Wed Apr 28 00:03:21 2010 New Revision: 158817 URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=158817 Log: PR c++/29043 * init.c (perform_member_init): check for uninitialized const or reference members, including array types. Added: trunk/gcc/testsuite/g++.dg/init/pr29043.C Modified: trunk/gcc/cp/ChangeLog trunk/gcc/cp/init.c trunk/gcc/testsuite/ChangeLog
Fixed.