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

            Bug ID: 127835
           Summary: unused expression result makes compiler allow invalid
                    code in constant evaluation
           Product: gcc
           Version: 17.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: irvsrc at gmail dot com
  Target Milestone: ---

The following invalid code compiles without errors:
```
struct ty {
    int n = 0;

    auto consteval fn() {
        n + 1;
    }
};

auto main() -> int {
    auto v = ty{};
    v.fn();
}
```

However, the call to `v.fn()` is ill-formed because:
1) The call to consteval function `ty::fn` is not a constant expression,
because the read of non-constexpr variable `v` is not allowed in a constant
evaluation.
2) Even if `v` is made a constexpr variable, then the `this` argument to member
function `fn` has type `const ty`, however the function is not const-qualified.

I believe this issue may be due to constant evaluation rules not being applied
to unused expression results.

This also occurs across many other GCC and C++ versions from what I have
tested.

GCC (trunk)
-std=c++26

Reply via email to