https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110981

            Bug ID: 110981
           Summary: constexpr variable definition that requires dynamic
                    destruction should be rejected
           Product: gcc
           Version: 13.2.1
            Status: UNCONFIRMED
          Keywords: accepts-invalid
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: de34 at live dot cn
  Target Milestone: ---

The following program is accepted by GCC and results in dynamic destruction
codes (because the lifetime-extended unique_ptr temporary object is not
trivially destructible and has no constant destruction)
(https://godbolt.org/z/8e9Y9E9jf):
```
#include <memory>

constexpr std::unique_ptr<int>&& r = {};

int main()
{
    r = std::make_unique<int>(42);
}
```

I think we should reject the definition of r due to CWG2529
(https://cplusplus.github.io/CWG/issues/2529.html).

Also, a slightly different example is also currently accepted by GCC
(https://godbolt.org/z/1rf768aKs):

```
#include <memory>

struct S {
    std::unique_ptr<int>&& r;
};

constexpr S s{{}};

int main()
{
    s.r = std::make_unique<int>(42);
}
```

This seemly also needs to be rejected, although there's an open CWG issue
(https://cplusplus.github.io/CWG/issues/2702.html).

The rationale should be that
- every lifetime-extended temporary object created in the initialization of a
constexpr variable needs to have constant destruction, and
- such a non-const temporary object is not usable in constant expressions, so
lvalue-to-rvalue conversion from any of its subobjects makes the destruction
non-constant.

Reply via email to