[Bug c++/97427] New: constexpr destructor for const object incorrectly rejected as modifying const object

ldalessandro at gmail dot com gcc-bugzilla@gcc.gnu.org
Wed Oct 14 19:14:43 GMT 2020


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

            Bug ID: 97427
           Summary: constexpr destructor for const object incorrectly
                    rejected as modifying const object
           Product: gcc
           Version: 11.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: ldalessandro at gmail dot com
  Target Milestone: ---

The destructor below is rejected (see https://godbolt.org/z/M8YxTY). Workaround
for now is to mark n as mutable.

```
struct Foo {
    int n = 1;
    constexpr ~Foo() {
        n = 0;
    }
};

constexpr bool foo() {
    const Foo b;
    return true;
}

static_assert(foo());
```

The write to n is flagged as below.


```
<source>:13:18: error: non-constant condition for static assertion
   13 | static_assert(foo());
      |               ~~~^~
<source>:13:18:   in 'constexpr' expansion of 'foo()'
<source>:9:15:   in 'constexpr' expansion of '((Foo*)(& b))->Foo::~Foo()'
<source>:4:11: error: modifying a const object '((Foo*)this)->Foo::n' is not
allowed in a constant expression
    4 |         n = 0;
```


More information about the Gcc-bugs mailing list