Bug 54922 - [C++11][DR 1359] constexpr constructors require initialization of all union members
Summary: [C++11][DR 1359] constexpr constructors require initialization of all union m...
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.8.0
: P3 normal
Target Milestone: 4.8.0
Assignee: Not yet assigned to anyone
URL:
Keywords: rejects-valid
: 54768 55226 56215 56583 (view as bug list)
Depends on: 55226
Blocks: constexpr
  Show dependency treegraph
 
Reported: 2012-10-13 17:36 UTC by Daniel Krügler
Modified: 2013-03-09 22:15 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2012-10-20 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Daniel Krügler 2012-10-13 17:36:25 UTC
gcc 4.8.0 20120930 (experimental) compiled with the flags

-Wall -pedantic -std=c++11

rejects the following code:

//----------------
lass nullable_int
{
  bool init_;
  union {
    unsigned char for_value_init;
    int value_;
  };
public:
  constexpr nullable_int() : init_(false), for_value_init() {}
};
//----------------

"main.cpp||In constructor 'constexpr nullable_int::nullable_int()':|
main.cpp|9|error: uninitialized member 'nullable_int::<anonymous>' in 'constexpr' constructor|"

This prevents a very reasonable use-case, namely the creation of a literal type that contains an anonymous union member.
Comment 1 Daniel Krügler 2012-10-13 20:54:14 UTC
(In reply to comment #0)
Some copy'n-paste error occurred while attempting to format the code example. The correct code under investigation was:

//----------------
class nullable_int
{
  bool init_;
  union {
    unsigned char for_value_init;
    int value_;
  };
public:
  constexpr nullable_int() : init_(false), for_value_init() {}
};
//----------------
Comment 2 Paolo Carlini 2012-10-20 23:21:42 UTC
Related to PR54768.
Comment 3 Paolo Carlini 2012-10-20 23:31:41 UTC
Related to PR51675.
Comment 4 Paolo Carlini 2012-10-22 12:42:32 UTC
On it.
Comment 5 paolo@gcc.gnu.org 2012-10-23 23:43:25 UTC
Author: paolo
Date: Tue Oct 23 23:43:21 2012
New Revision: 192749

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=192749
Log:
/cp
2012-10-23  Paolo Carlini  <paolo.carlini@oracle.com>

	PR c++/54922
	* semantics.c (cx_check_missing_mem_inits): Handle anonymous union
	members.

/testsuite
2012-10-23  Paolo Carlini  <paolo.carlini@oracle.com>

	PR c++/54922
	* g++.dg/cpp0x/constexpr-union4.C: New.

Added:
    trunk/gcc/testsuite/g++.dg/cpp0x/constexpr-union4.C
Modified:
    trunk/gcc/cp/ChangeLog
    trunk/gcc/cp/semantics.c
    trunk/gcc/testsuite/ChangeLog
Comment 6 Paolo Carlini 2012-10-23 23:44:27 UTC
Fixed for 4.8.0.
Comment 7 Paolo Carlini 2012-11-07 11:05:20 UTC
Patch is reverted for now. The fix wasn't correct (or was incomplete), as showed by the testcase:

class nullable_int
{
  bool init_;
  union {
    unsigned char for_value_init;
    int value_;
  };
public:
  constexpr nullable_int() : init_(false), for_value_init() {}
};

int main()
{
  nullable_int n;
}
Comment 8 paolo@gcc.gnu.org 2012-11-07 11:15:50 UTC
Author: paolo
Date: Wed Nov  7 11:15:40 2012
New Revision: 193292

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=193292
Log:
/cp
2012-11-07  Paolo Carlini  <paolo.carlini@oracle.com>

	PR c++/55226
	Revert:
	2012-10-23  Paolo Carlini  <paolo.carlini@oracle.com>

	PR c++/54922
	* semantics.c (cx_check_missing_mem_inits): Handle anonymous union
	members.

/testsuite
2012-11-07  Paolo Carlini  <paolo.carlini@oracle.com>

	PR c++/55226
	Revert:
	2012-10-23  Paolo Carlini  <paolo.carlini@oracle.com>

	PR c++/54922
	* g++.dg/cpp0x/constexpr-union4.C: New.

Removed:
    trunk/gcc/testsuite/g++.dg/cpp0x/constexpr-union4.C
Modified:
    trunk/gcc/cp/ChangeLog
    trunk/gcc/cp/semantics.c
    trunk/gcc/testsuite/ChangeLog
Comment 9 Paolo Carlini 2012-11-07 11:16:37 UTC
*** Bug 55226 has been marked as a duplicate of this bug. ***
Comment 10 Paolo Carlini 2013-02-06 09:34:07 UTC
*** Bug 56215 has been marked as a duplicate of this bug. ***
Comment 11 Jason Merrill 2013-02-15 01:27:52 UTC
Author: jason
Date: Fri Feb 15 01:27:47 2013
New Revision: 196070

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=196070
Log:
	PR c++/54922
	* semantics.c (build_anon_member_initialization): New.
	(build_data_member_initialization): Use it.

Added:
    trunk/gcc/testsuite/g++.dg/cpp0x/constexpr-union4.C
    trunk/gcc/testsuite/g++.dg/cpp0x/constexpr-union5.C
Modified:
    trunk/gcc/cp/ChangeLog
    trunk/gcc/cp/semantics.c
Comment 12 Paolo Carlini 2013-02-15 11:23:21 UTC
Fixed.
Comment 13 Jason Merrill 2013-02-15 19:37:57 UTC
*** Bug 54768 has been marked as a duplicate of this bug. ***
Comment 14 Paolo Carlini 2013-03-09 22:15:47 UTC
*** Bug 56583 has been marked as a duplicate of this bug. ***