Skip to content

[dcl.constexpr] Uninitialized non-static data members of fundamental type CWG2558 #5422

Closed
@xmh0511

Description

@xmh0511

Consider this example

struct A{
   constexpr A(){}
   int value;
};
constexpr A obj;  // #1

In C++17 standard, we have the restriction on the constructor that is declared with constexpr. [dcl.constexpr] p4

The definition of a constexpr constructor shall satisfy the following requirements:

  • [...]
  • every non-variant non-static data member and base class subobject shall be initialized

Since the member value does not have a default initializer or specified its initialization with the mem-initializer, thus its default initialization performs no initialization. From this perspective, we can justify that #1 is ill-formed. However, in the current draft, we do not have this restriction anymore, and I do not find any replaceable rules. The full-expression of the initialization does not violate [expr.const] p5 nor [expr.const] p11, however, the example is rejected by all implementations. The reason for the rejection is basically:

subobject of type 'int' is not initialized
'A()' is not a constant expression because it refers to an incompletely initialized variable

We do not have the restrictions in either [dcl.constexpr] or [expr.const]. Is the revision of the draft omit the restriction?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions