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

            Bug ID: 87275
           Summary: unsequenced writes not diagnosed in constant
                    expression
           Product: gcc
           Version: 8.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: bbi5291 at gmail dot com
  Target Milestone: ---

An expression fails to be a constant expression if it contains UB. The code
below contains a function whose evaluation results in UB when called with the
argument `true` because of unsequenced writes to `x`. However, the code
compiles without a diagnostic when said function is called in a context that
requires a constant expression.

$ g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/local/libexec/gcc/x86_64-pc-linux-gnu/8.1.0/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: ./configure --prefix=/usr/local --disable-multilib
--without-mpc --without-mpfr --without-gmp --without-cloog --without-isl
--enable-languages=c,c++
Thread model: posix
gcc version 8.1.0 (GCC) 

$ g++ -std=c++17 -Wall -pedantic main.cpp

$ cat main.cpp
constexpr int f(bool b) {
    int x = 0, y = 0;
    int& r = b ? x : y;
    return (x++) + (r++);
}
template <int x> struct S {};
int main() {
    S<f(true)>();
    S<f(false)>();
}

Reply via email to