[Bug c++/48859] New: Regression: incorrect "uninitialized const member" error on new without new-initializer
jyasskin at gcc dot gnu.org
gcc-bugzilla@gcc.gnu.org
Tue May 3 23:56:00 GMT 2011
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.)
More information about the Gcc-bugs
mailing list