Bug 38232 - [4.4 Regression] "value-initialization of reference" warning too strict
Summary: [4.4 Regression] "value-initialization of reference" warning too strict
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.4.0
: P3 normal
Target Milestone: 4.4.0
Assignee: Jason Merrill
URL:
Keywords: rejects-valid
Depends on:
Blocks:
 
Reported: 2008-11-23 07:36 UTC by Chris Demetriou
Modified: 2008-12-04 14:29 UTC (History)
4 users (show)

See Also:
Host: i686-linux
Target: i686-linux
Build: i686-linux
Known to work: 4.3.2
Known to fail:
Last reconfirmed: 2008-12-03 20:57:38


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Chris Demetriou 2008-11-23 07:36:57 UTC
"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)
Comment 1 Richard Biener 2008-11-23 12:20:30 UTC
Confirmed.
Comment 2 Andrew Pinski 2008-11-23 18:31:13 UTC
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.
Comment 3 Lawrence Crowl 2008-11-23 23:50:19 UTC
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.

Comment 4 Chris Demetriou 2008-12-01 23:28:41 UTC
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.)
Comment 5 Jason Merrill 2008-12-03 23:58:46 UTC
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

Comment 6 Chris Demetriou 2008-12-04 07:12:38 UTC
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.)
Comment 7 Jason Merrill 2008-12-04 14:29:12 UTC
Just hadn't gotten to that part yet, thanks for the reminder.  Fixed.