Bug 110570 - Error reading mutable subobject in constexpr when object lifetime began within the evaluation of E
Summary: Error reading mutable subobject in constexpr when object lifetime began withi...
Status: UNCONFIRMED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 13.1.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: rejects-valid
Depends on:
Blocks:
 
Reported: 2023-07-06 07:31 UTC by Fedor Chelnokov
Modified: 2023-07-07 01:51 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Fedor Chelnokov 2023-07-06 07:31:44 UTC
This program

struct S {
    mutable int i = 2;
};

constexpr auto f = []{
    constexpr S s;
    s.i = 3;
    return s.i;
};

static_assert( f() == 3 );

is accepted in Clang, MSVC and ICC.

But GCC rejects the code with the error

  in 'constexpr' expansion of 'f.<lambda()>()'
error: mutable 'S::i' is not usable in a constant expression

Online demo: https://gcc.godbolt.org/z/Wsfeah3hq

It seems wrong, since lifetime of object s begins within the evaluation of constant expression, see https://timsong-cpp.github.io/cppwp/n4861/expr.const#5.16

Clang developers think that GCC is wrong here: https://github.com/llvm/llvm-project/issues/63695

SO question: https://stackoverflow.com/q/76608087/7325599