Bug 57820 - [DR 253] NSDMI and const objects
Summary: [DR 253] NSDMI and const objects
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.8.1
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: accepts-invalid
Depends on:
Blocks: constexpr NSDMI c++-core-issues
  Show dependency treegraph
 
Reported: 2013-07-04 09:45 UTC by liwei
Modified: 2024-04-04 00:21 UTC (History)
4 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2013-07-04 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description liwei 2013-07-04 09:45:24 UTC
the source code:

struct C
{
    int a = 2;
    int b = a + 1;
};

C c;  //OK
constexpr C d; //error

int main()
{
}

Why I can't initialize d?
Comment 1 Jonathan Wakely 2013-07-04 09:57:02 UTC
I think it's invalid unless you provide an initializer or the class has a default constructor e.g.

constexpr C d = {};

G++ still rejects that though, with a very unhelpful diagnostic:

t.cc:8:18: error: ‘constexpr C::C()’ called in a constant expression
 constexpr C d = {}; //error
                  ^
t.cc:1:8: note: ‘constexpr C::C()’ is not usable as a constexpr function because:
 struct C
        ^
Comment 2 Harald van Dijk 2013-07-04 17:46:01 UTC
Adding an user-defined constexpr C() {} makes it fail more clearly:

struct C
{
    int a = 2;
    int b = a + 1;

    constexpr C() {}
};


test.cc: In constructor ‘constexpr C::C()’:
test.cc:6:20: sorry, unimplemented: use of the value of the object being constructed in a constant expression
     constexpr C() {}

and indeed, if `int b = a + 1;` is changed to `int b = 3;`, G++ accepts it.
Comment 3 Paolo Carlini 2014-11-03 10:40:54 UTC
AFAICS, in mainline this is now an accepts-invalid, because we accept submitter's testcase too as-is.

I'm going to add to the testsuite the valid variants in Comments 1 & 2.
Comment 4 paolo@gcc.gnu.org 2014-11-03 10:49:36 UTC
Author: paolo
Date: Mon Nov  3 10:49:05 2014
New Revision: 217035

URL: https://gcc.gnu.org/viewcvs?rev=217035&root=gcc&view=rev
Log:
2014-11-03  Paolo Carlini  <paolo.carlini@oracle.com>

	PR c++/57820
	* g++.dg/cpp0x/constexpr-ctor16.C: New.
	* g++.dg/cpp0x/constexpr-ctor17.C: Likewise.

Added:
    trunk/gcc/testsuite/g++.dg/cpp0x/constexpr-ctor16.C
    trunk/gcc/testsuite/g++.dg/cpp0x/constexpr-ctor17.C
Modified:
    trunk/gcc/testsuite/ChangeLog
Comment 5 Jason Merrill 2014-11-20 12:57:23 UTC
This issue is DR 253, which has not been resolved, but the committee seems inclined to allow this code, so I'm not going to change G++ at this time.

http://www.open-std.org/JTC1/SC22/WG21/docs/cwg_toc.html#253
Comment 6 Andrew Pinski 2024-04-04 00:21:07 UTC
[Adopted at the November, 2016 meeting as part of paper P0490R0.]