"gcc version 4.4.0 20081123 (experimental) (GCC)" rejects the code: class base { public: base(); virtual ~base(); private: int& int_ref; // initialized by base ctor, not visible here }; class derived : public base { }; base *make_derived() { return new derived(); } with the error: test.cc: In function 'base* make_derived()': test.cc:14: error: value-initialization of reference 4.3.2 accepts this code, the Comeau test drive accepts it, and AFAICT there's nothing wrong with it. Adding a user-supplied default ctor to 'derived' fixes it. This was build from a GCC trunk checkout as of this evening: Using built-in specs. Target: i686-linux Configured with: ../trunk/configure --enable-languages=c,c++ --build=i686-linux --host=i686-linux --target=i686-linux --prefix=/g/users/cgd/proj/gcc-trunk/bld/../inst Thread model: posix gcc version 4.4.0 20081123 (experimental) (GCC)
Confirmed.
DR 178 changed some of the behavior here and I think what GCC is doing is now the correct behavior according to that Defect report. http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#178 See Also PR 36394 and PR 11309.
The relevant change in DR 178 is To value-initialize an object of type T means: * if T is a class type (clause 9 [class]) with a user-declared constructor (12.1 [class.ctor]), then the default constructor for T is called (and the initialization is ill-formed if T has no accessible default constructor); The default constructor for derived should call the default constructor for base. The base constructor is defined in another file, and so any incorrect code in the base constructor should be flagged in another compilation.
Decided to try to verify again with current head of trunk compiler (since several other bugs I reported have been fixed, and one was related to value initialization). gcc version 4.4.0 20081201 (experimental) (GCC) configured/built same way as before. This error still occurs. (FYI.)
Subject: Bug 38232 Author: jason Date: Wed Dec 3 23:57:19 2008 New Revision: 142418 URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=142418 Log: PR c++/38232 * init.c (build_value_init): Do initial zero-initialization of a class with an implicitly-defined constructor using build_zero_init rather than in build_value_init. (build_value_init_1): Fold into build_value_init. Added: trunk/gcc/testsuite/g++.dg/init/value5.C Modified: trunk/gcc/cp/ChangeLog trunk/gcc/cp/init.c trunk/gcc/testsuite/ChangeLog
verified after syncing that my test case is now fixed. (Would close, but not sure why Jason didn't close it... please close if there's nothing else to do, Jason, or tell me to.)
Just hadn't gotten to that part yet, thanks for the reminder. Fixed.