Fed*_*dor 5 c++ language-lawyer constexpr
如果constexpr引用仅使用另一个对象初始化,constexpr如示例所示:
int main() {
constexpr int a = 0;
constexpr const int & b = a;
}
Run Code Online (Sandbox Code Playgroud)
然后 GCC 和 Clang 都拒绝了它constexpr variable 'b' must be initialized by a constant expression, address of non-static constexpr variable 'a' may differ on each invocation of the enclosing function; add 'static' to give it a constant address。
同时MSVC接受该示例。演示: https: //gcc.godbolt.org/z/Whv7YeWKW
添加到以下内容后,所有编译器都会接受代码static:
static constexpr int a = 0;
Run Code Online (Sandbox Code Playgroud)
演示: https: //gcc.godbolt.org/z/abeP4z64E
标准是否真的要求任何constexpr引用只能使用静态对象进行初始化?