https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105696
Bug ID: 105696 Summary: invalid use of constexpr static class member accepted as constant expression Product: gcc Version: 12.1.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: yiz152 at ucsd dot edu Target Milestone: --- Created attachment 53017 --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=53017&action=edit Test case G++ 12.1.0 accepts constexpr static class members referenced using class member access syntax (see below/attached). The working draft states (https://eel.is/c++draft/class.static#general-1) that in such cases, the object expression (this->val) is evaluated, thereby breaking constexpr-ness (cf. https://eel.is/c++draft/expr.const#5.1). Compiled/tested with gcc 12.1.0, clang 13.0.1, -pedantic -Wall -Wextra -std=c++1z. Also tested with -std=c++11,-std=c++14. Code/error (same as the attached file): #include <type_traits> struct A { constexpr static int a = 17; }; struct B { A val; int foo() { // gcc compiles without warning; clang rejects return std::integral_constant<int, val.a>::value; } int good_foo() { // gcc compiles without warning, correct return std::integral_constant<int, decltype(val)::a>::value; } void bar() { char a[val.a]; // gcc compiles without warning, clang warns -Wvla (void) a; } void var_bar(int len) { char a[len]; // gcc compiles with -Wvla, clang does as well (void) a; } }; int main() {} gcc -v: Using built-in specs. COLLECT_GCC=g++ COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-pc-linux-gnu/12.1.0/lto-wrapper Target: x86_64-pc-linux-gnu Configured with: /build/gcc/src/gcc/configure --enable-languages=c,c++,ada,fortran,go,lto,objc,obj-c++ --enable-bootstrap --prefix=/usr --libdir=/usr/lib --libexecdir=/usr/lib --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=https://bugs.archlinux.org/ --with-linker-hash-style=gnu --with-system-zlib --enable-__cxa_atexit --enable-cet=auto --enable-checking=release --enable-clocale=gnu --enable-default-pie --enable-default-ssp --enable-gnu-indirect-function --enable-gnu-unique-object --enable-linker-build-id --enable-lto --enable-multilib --enable-plugin --enable-shared --enable-threads=posix --disable-libssp --disable-libstdcxx-pch --disable-werror --with-build-config=bootstrap-lto --enable-link-serialization=1 Thread model: posix Supported LTO compression algorithms: zlib zstd gcc version 12.1.0 (GCC)