The following code only compiles on GCC (checked it on 10.4 and 13.2 on godbolt.org) but not on Clang (fails on all versions I tried, for example 17.0.1 on godbolt.org):
struct A { static constexpr int b{1}; }; int main(int argc, char *argv[]) { A a; A& aref{a}; constexpr auto bb1{a.b}; constexpr auto bb2{aref.b}; return bb1+bb2; } Clang outputs:
<source>:9:20: error: constexpr variable 'bb2' must be initialized by a constant expression 9 | constexpr auto bb2{aref.b}; | ^ ~~~~~~~~ <source>:9:24: note: initializer of 'aref' is not a constant expression 9 | constexpr auto bb2{aref.b}; | ^ <source>:7:14: note: declared here 7 | A& aref{a}; | https://godbolt.org/z/nG4j3KefE
Why?
A::bis a static field which is referenced by.(dot).