在Bjarne Stroustrup的书“ C ++编程语言(第4版)”中,第110页。267(第10.4.5节“地址常量表达式”)中,他使用了一个代码示例,其中将局部变量的地址设置为constexpr变量。我以为这看起来很奇怪,所以我尝试使用g ++ 7.3.0版运行示例,但无法获得相同的结果。这是他的逐字代码示例(尽管略有删节):
extern char glob;
void f(char loc) {
constexpr const char* p0 = &glob; // OK: &glob's is a constant
constexpr const char* p2 = &loc; // OK: &loc is constant in its scope
}
Run Code Online (Sandbox Code Playgroud)
运行此命令时,我得到:
error: ‘(const char*)(& loc)’ is not a constant expression
Run Code Online (Sandbox Code Playgroud)
g ++发生了我不知道的事情,还是Bjarne的示例有更多事情?