Title
Subtle difference between constant-initialized and constexpr
Status
dr
Section
7.7 [expr.const]
Submitter
CWG

Created on 2024-06-24.00:00:00 last changed 4 days ago

Messages

Date: 2024-10-25.21:02:00

Proposed resolution (approved by CWG 2024-10-25):

Change in 7.7 [expr.const] paragraph 2 as follows:

A variable or temporary object o is constant-initialized if
  • either it has an initializer or its default-initialization results in some initialization being performed type is const-default-constructible (9.4.1 [dcl.init.general]), and
  • ...
Date: 2024-11-15.00:00:00

[Accepted as a DR at the November, 2024 meeting.]

Subclause 7.7 [expr.const] paragraph 2 defines "constant-initialized" using the following rule:

A variable or temporary object o is constant-initialized if
  • either it has an initializer or its default-initialization results in some initialization being performed, and
  • ...

However, the rules for constexpr are slightly different, per 9.2.6 [dcl.constexpr] paragraph 6:

A constexpr specifier used in an object declaration declares the object as const. Such an object shall have literal type and shall be initialized. ...

The difference manifests for an example such as:

  struct S {};
  int main() {
    constexpr S s;       // OK
    constexpr S s2 = s;  // error: s is not constant-initialized
  }

Is the difference intentional?

History
Date User Action Args
2024-12-17 09:54:02adminsetstatus: ready -> dr
2024-11-19 12:01:24adminsetstatus: tentatively ready -> ready
2024-10-25 21:02:00adminsetstatus: review -> tentatively ready
2024-10-11 20:31:34adminsetmessages: + msg7837
2024-10-11 20:31:34adminsetstatus: open -> review
2024-06-24 00:00:00admincreate