https://gcc.gnu.org/bugzilla/show_bug.cgi?id=83116
Bug ID: 83116 Summary: Statement with no effect causes wrong code of static object constexpr method Product: gcc Version: 7.2.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: gcc at gajoch dot pl Target Milestone: --- Created attachment 42685 --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=42685&action=edit Preprocessed source Following testcase: $ cat main.cpp #include <cstdio> struct foo { constexpr foo() : size_(0) { (void)size(); } constexpr int size() { return size_; } int size_; }; int main() { static foo var; var.size_ = 5; std::printf("%d %d", var.size_, var.size()); return 0; } $ g++ -Wall -Wextra -pedantic -O0 main.cpp -o main && ./main 5 5 $ g++ -Wall -Wextra -pedantic -O1 main.cpp -o main && ./main 5 0 (Same happens on -Og, -O2 and -O3) Tested on: $ g++ -v Using built-in specs. COLLECT_GCC=g++ COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-pc-linux-gnu/7.2.0/lto-wrapper Target: x86_64-pc-linux-gnu Configured with: /build/gcc/src/gcc/configure --prefix=/usr --libdir=/usr/lib --libexecdir=/usr/lib --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=https://bugs.archlinux.org/ --enable-languages=c,c++,ada,fortran,go,lto,objc,obj-c++ --enable-shared --enable-threads=posix --enable-libmpx --with-system-zlib --with-isl --enable-__cxa_atexit --disable-libunwind-exceptions --enable-clocale=gnu --disable-libstdcxx-pch --disable-libssp --enable-gnu-unique-object --enable-linker-build-id --enable-lto --enable-plugin --enable-install-libiberty --with-linker-hash-style=gnu --enable-gnu-indirect-function --disable-multilib --disable-werror --enable-checking=release --enable-default-pie --enable-default-ssp Thread model: posix gcc version 7.2.0 (GCC) Correct behavior is seen using gcc-5.4, clang and msvc. Removing constructor body gives correct code - this is stripped version, originally there was assert(this->size() >= 0); UBSAN gives no results. This bug occurs also on 6.1.0, 6.2.0, 6.3.0, 7.1.0, 7.2.0 and today's trunk (8.0.0 20171122), as tested on compiler explorer: https://godbolt.org/g/hr4Nq4