Bug 42844 - const variable requires initializer / no explicitly declared default constructor
Summary: const variable requires initializer / no explicitly declared default constructor
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: unknown
: P3 normal
Target Milestone: ---
Assignee: Fabien Chene
URL:
Keywords: accepts-invalid
Depends on:
Blocks:
 
Reported: 2010-01-22 11:27 UTC by Mathieu Malaterre
Modified: 2014-02-20 08:44 UTC (History)
5 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail: 4.1.2, 4.4.2
Last reconfirmed: 2010-01-22 11:57:37


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Mathieu Malaterre 2010-01-22 11:27:22 UTC
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;
Comment 1 Jonathan Wakely 2010-01-22 11:57:37 UTC
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.
Comment 2 Jonathan Wakely 2010-01-22 12:27:01 UTC
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;
Comment 3 Andrew Pinski 2010-01-22 15:56:29 UTC
Related to PR 25811.
Comment 4 Andrew Pinski 2010-01-22 15:56:48 UTC
And PR 29043.
Comment 5 Fabien Chene 2010-03-30 11:40:44 UTC
I'm working on this bug
Comment 6 Dodji Seketeli 2010-03-30 11:44:21 UTC
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.

Comment 7 Fabien Chene 2010-03-30 11:51:24 UTC
(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 :-)

Comment 8 Jonathan Wakely 2010-03-30 11:56:21 UTC
you should have the option "Accept bug" below the comment box.  I've done it for you
Comment 9 Fabien Chene 2010-03-30 14:11:24 UTC
(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;
}




Comment 10 Fabien Chene 2010-03-30 16:47:22 UTC
> reduced testcase for this one:
> 
> struct A {};
> 
> void f()
> {
>   A const a;
> }

I misspoke, let's keep the original testcase.

Comment 11 Fabien Chene 2010-04-12 20:45:27 UTC
Patch here:
http://gcc.gnu.org/ml/gcc-patches/2010-04/msg00604.html
Comment 12 Fabien Chene 2010-04-20 22:49:20 UTC
updated patch here:
http://gcc.gnu.org/ml/gcc-patches/2010-04/msg01148.html
Comment 13 Jason Merrill 2010-04-27 18:56:36 UTC
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

Comment 14 Fabien Chene 2010-11-12 14:27:36 UTC
Fixed.
Comment 15 Jason Merrill 2011-09-23 21:30:52 UTC
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
Comment 16 Jason Merrill 2011-09-25 20:29:09 UTC
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
Comment 17 David Krauss 2014-02-20 08:44:42 UTC
The changelog should refer to CWG DR 253, not 234.