public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/48859] New: Regression: incorrect "uninitialized const member" error on new without new-initializer
@ 2011-05-03 23:56 jyasskin at gcc dot gnu.org
  2011-05-04  0:03 ` [Bug c++/48859] " pinskia at gcc dot gnu.org
                   ` (9 more replies)
  0 siblings, 10 replies; 11+ messages in thread
From: jyasskin at gcc dot gnu.org @ 2011-05-03 23:56 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=48859

           Summary: Regression: incorrect "uninitialized const member"
                    error on new without new-initializer
           Product: gcc
           Version: 4.6.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: jyasskin@gcc.gnu.org


$ cat test.cc
struct HasConstructor {
  HasConstructor() {}
};

class ConstMember {
  const HasConstructor empty_;
};

void foo() {
  new ConstMember;
}
$ g++-mp-4.5 -c test.cc
$ g++-mp-4.6 -c test.cc
test.cc: In function 'void foo()':
test.cc:10:7: error: uninitialized const member in 'class ConstMember' using
'new' without new-initializer
test.cc:6:24: note: 'ConstMember::empty_' should be initialized
$ 


[expr.new]p15 says, "If the new-initializer is omitted: If T is a (possibly
cv-qualified) non-POD class type (or array thereof), the object is
default-initialized (8.5) If T is a const-qualified type, the underlying class
type shall have a user-declared default constructor."

T is ConstMember and is not const-qualified, so the object is
default-initialized.

[dcl.init]p5 says, "To default-initialize an object of type T means: if T is a
non-POD class type (clause 9), the default constructor for T is called (and the
initialization is ill-formed if T has no accessible default constructor);"

[class.ctor]p7 says, "The implicitly-defined default constructor performs the
set of initializations of the class that would be performed by a user-written
default constructor for that class with an empty mem- initializer-list (12.6.2)
and an empty function body. If that user-written default constructor would be
ill- formed, the program is ill-formed."

The empty user-written default constructor for ConstMember is well-formed, and
I don't see any other wording saying that const members prevent the creation of
a default constructor. Further, a local variable with no initializer
successfully compiles. So I believe gcc-4.6 is wrong here and gcc-4.5 was
right.

I don't see anything in the C++0x draft that would change the right behavior
here.

The workaround, of course, is to define the empty default constructor in
ConstMember. (In C++0x mode, "=default" does not work around the problem.)


^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2011-05-11  6:09 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-05-03 23:56 [Bug c++/48859] New: Regression: incorrect "uninitialized const member" error on new without new-initializer jyasskin at gcc dot gnu.org
2011-05-04  0:03 ` [Bug c++/48859] " pinskia at gcc dot gnu.org
2011-05-04 12:35 ` [Bug c++/48859] [4.6/4.7 Regression] " redi at gcc dot gnu.org
2011-05-04 12:36 ` redi at gcc dot gnu.org
2011-05-04 13:52 ` fabien at gcc dot gnu.org
2011-05-09 17:52 ` fabien at gcc dot gnu.org
2011-05-09 18:28 ` redi at gcc dot gnu.org
2011-05-10  8:27 ` fabien at gcc dot gnu.org
2011-05-10 15:47 ` jyasskin at gcc dot gnu.org
2011-05-10 16:38 ` fabien at gcc dot gnu.org
2011-05-11  7:54 ` fabien at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).