Bug 29043 - Constructor for POD type with const member without member initializer accepted
Summary: Constructor for POD type with const member without member initializer accepted
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.2.0
: P3 normal
Target Milestone: ---
Assignee: Fabien Chene
URL:
Keywords: accepts-invalid
Depends on:
Blocks: 29843
  Show dependency treegraph
 
Reported: 2006-09-12 21:02 UTC by Jorn Wolfgang Rennecke
Modified: 2010-11-12 14:25 UTC (History)
5 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2006-09-13 16:10:45


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Jorn Wolfgang Rennecke 2006-09-12 21:02:14 UTC
The following C++ code should not compile:

class C
{
  C() { }
  struct s {
    const int i;
  };
};


The C++ standard clause 12.6.2 ; 4 says that for each non-static data
member of a POD class type containing a member of a const-qualified type, there
must be a member initialiser, or else the program is ill-formed.
Comment 1 David Fang 2006-09-13 03:00:04 UTC
As you've written it, class C doesn't have any non-static members.  Struct C::s hasn't been declared as a member object of C.  const int i is a member of C::s, not C, so C() without member initializers should be acceptable.  

Note, if you write just :

struct s {
  const int i;
};

// This, however, doesn't give any diagnostics until an instance of s is declared.  

s foo;

struct.cc:7: error: structure 'foo' with uninitialized const members

Now, I'd need to lookup the standard to determine whether or not a diagnostic is appropriate...
Comment 2 Andrew Stubbs 2006-09-13 09:23:05 UTC
(In reply to comment #1)
> As you've written it, class C doesn't have any non-static members.  Struct C::s
> hasn't been declared as a member object of C.  const int i is a member of C::s,
> not C, so C() without member initializers should be acceptable.  

How about this example:

struct S {
  const int i;
};

class C
{
public:
  C() { }    
  S s;
};

void f()
{
  C c;
  S s;
}

This fails at the line `S s;' in f(), but the `C c;' line is accepted silently.

The standard says the requirement applies to data-members *containing* a member of const-qualified type.
Comment 3 Wolfgang Bangerth 2006-09-13 16:10:45 UTC
Confirmed with the testcase from attachment #2 [details].
Comment 4 Fabien Chene 2010-04-19 20:03:40 UTC
mine ...
Comment 5 Fabien Chene 2010-04-20 22:47:24 UTC
patch here:
http://gcc.gnu.org/ml/gcc-patches/2010-04/msg01269.html
Comment 6 Jason Merrill 2010-04-28 00:03:36 UTC
Subject: Bug 29043

Author: jason
Date: Wed Apr 28 00:03:21 2010
New Revision: 158817

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=158817
Log:
	PR c++/29043
	* init.c (perform_member_init): check for uninitialized const or
	reference members, including array types.

Added:
    trunk/gcc/testsuite/g++.dg/init/pr29043.C
Modified:
    trunk/gcc/cp/ChangeLog
    trunk/gcc/cp/init.c
    trunk/gcc/testsuite/ChangeLog

Comment 7 Fabien Chene 2010-11-12 14:25:53 UTC
Fixed.