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

            Bug ID: 116695
           Summary: Undefined behaviour involving unsequenced side effects
                    on a memory location during constant evaluation should
                    not compile
           Product: gcc
           Version: 15.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: pkeir at outlook dot com
  Target Milestone: ---

The C++14 code below compiles, but should not, as the constant expression
contains undefined behaviour:

constexpr bool ub()
{
  int i = 0;
  i = i++ + i; // UB

  return i==1;
}

constexpr bool b = ub();

A core constant expression should not contain an operation that would have
undefined or erroneous behaviour (https://eel.is/c++draft/expr.const#5.8).

The above code includes undefined behaviour as it contains side effects on a
memory location which is unsequenced relative to both another side effect on
the same memory location and a value computation using the value of the object
in the same memory location (http://eel.is/c++draft/intro.execution#10).

The compiler output can be viewed at https://godbolt.org/z/7vz4r5sf1

Reply via email to