I believe the following c++ code is incorrect: struct A { A(){} }; struct B : public A { }; int main() { const B b; return 0; } It fails with Comeau with the following error: "ComeauTest.c", line 13: error: const variable "b" requires an initializer -- class "B" has no explicitly declared default constructor const B b;
Confirmed, not a regression. [dcl.init]/9: If no initializer is specified for an object, and the object is of (possibly cv-qualified) non-POD class type (or array thereof), the object shall be default-initialized; if the object is of const-qualified type, the underlying class type shall have a user-declared default constructor.
N.B. the wording has moved in C++0x but the requirement is similar: 6 ... If a program calls for the default initialization of an object of a const-qualified type T, T shall be a class type with a user-provided default constructor. 11 If no initializer is specified for an object, the object is default-initialized;
Related to PR 25811.
And PR 29043.
I'm working on this bug
Subject: Re: const variable requires initializer / no explicitly declared default constructor > I'm working on this bug Could you please assign it to yourself then? Thanks.
(In reply to comment #6) > Subject: Re: const variable requires initializer / no > explicitly declared default constructor > > > I'm working on this bug > > Could you please assign it to yourself then? > Thanks. I would be glad to know how to do that :-)
you should have the option "Accept bug" below the comment box. I've done it for you
(In reply to comment #8) > you should have the option "Accept bug" below the comment box. > I've done it for you I've just seen it since you did it for me (thanks), but I still can't do it by myself for other bugs :-( reduced testcase for this one: struct A {}; void f() { A const a; }
> reduced testcase for this one: > > struct A {}; > > void f() > { > A const a; > } I misspoke, let's keep the original testcase.
Patch here: http://gcc.gnu.org/ml/gcc-patches/2010-04/msg00604.html
updated patch here: http://gcc.gnu.org/ml/gcc-patches/2010-04/msg01148.html
Subject: Bug 42844 Author: jason Date: Tue Apr 27 18:56:13 2010 New Revision: 158797 URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=158797 Log: PR c++/42844 * decl.c (check_for_uninitialized_const_var): Handle classes that need constructing, too. (check_initializer): Call it for classes that need constructing, too. * class.c (in_class_defaulted_default_constructor): New. * cp-tree.h: Declare it. Added: trunk/gcc/testsuite/g++.dg/cpp0x/pr42844-2.C trunk/gcc/testsuite/g++.dg/init/pr42844.C Modified: trunk/gcc/cp/ChangeLog trunk/gcc/cp/class.c trunk/gcc/cp/cp-tree.h trunk/gcc/cp/decl.c trunk/gcc/testsuite/ChangeLog trunk/gcc/testsuite/g++.dg/cpp0x/defaulted2.C trunk/gcc/testsuite/g++.dg/tree-ssa/pr27549.C trunk/gcc/testsuite/g++.old-deja/g++.mike/dyncast8.C trunk/libstdc++-v3/ChangeLog trunk/libstdc++-v3/src/future.cc trunk/libstdc++-v3/src/system_error.cc trunk/libstdc++-v3/testsuite/util/testsuite_error.h
Fixed.
Author: jason Date: Fri Sep 23 21:30:48 2011 New Revision: 179130 URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=179130 Log: Core 234 - allow const objects with no initializer or user-provided default constructor if the defaulted constructor initializes all the subobjects. PR c++/20039 PR c++/42844 * class.c (default_init_uninitialized_part): New. * cp-tree.h: Declare it. * decl.c (check_for_uninitialized_const_var): Use it. * init.c (perform_member_init): Likewise. (build_new_1): Likewise. * method.c (walk_field_subobs): Likewise. Added: trunk/gcc/testsuite/g++.dg/init/const8.C Modified: trunk/gcc/cp/ChangeLog trunk/gcc/cp/class.c trunk/gcc/cp/cp-tree.h trunk/gcc/cp/decl.c trunk/gcc/cp/init.c trunk/gcc/cp/method.c trunk/gcc/testsuite/ChangeLog trunk/gcc/testsuite/g++.dg/cpp0x/constexpr-object1.C trunk/gcc/testsuite/g++.dg/cpp0x/defaulted2.C trunk/gcc/testsuite/g++.dg/cpp0x/pr42844-2.C trunk/gcc/testsuite/g++.dg/init/pr20039.C trunk/gcc/testsuite/g++.dg/init/pr42844.C
Author: jason Date: Sun Sep 25 20:29:04 2011 New Revision: 179170 URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=179170 Log: Core 234 - allow const objects with no initializer or user-provided default constructor if the defaulted constructor initializes all the subobjects. PR c++/20039 PR c++/42844 * class.c (default_init_uninitialized_part): New. * cp-tree.h: Declare it. * decl.c (check_for_uninitialized_const_var): Use it. * init.c (perform_member_init): Likewise. (build_new_1): Likewise. * method.c (walk_field_subobs): Likewise. Added: branches/gcc-4_6-branch/gcc/testsuite/g++.dg/init/const8.C Modified: branches/gcc-4_6-branch/gcc/cp/ChangeLog branches/gcc-4_6-branch/gcc/cp/class.c branches/gcc-4_6-branch/gcc/cp/cp-tree.h branches/gcc-4_6-branch/gcc/cp/decl.c branches/gcc-4_6-branch/gcc/cp/init.c branches/gcc-4_6-branch/gcc/cp/method.c branches/gcc-4_6-branch/gcc/testsuite/ChangeLog branches/gcc-4_6-branch/gcc/testsuite/g++.dg/cpp0x/constexpr-object1.C branches/gcc-4_6-branch/gcc/testsuite/g++.dg/cpp0x/defaulted2.C branches/gcc-4_6-branch/gcc/testsuite/g++.dg/cpp0x/pr42844-2.C branches/gcc-4_6-branch/gcc/testsuite/g++.dg/init/pr42844.C branches/gcc-4_6-branch/gcc/testsuite/lib/prune.exp
The changelog should refer to CWG DR 253, not 234.