【发布时间】:2014-03-05 14:35:59
【问题描述】:
考虑以下代码:
constexpr const int A = 42;
const int &B = A;
static_assert(&A == &B, "Bug");
constexpr const int &C = B;
static_assert(&A == &C, "Bug");
int main() { return 0; }
clang 版本 3.3 完全接受它,而 g++ (SUSE Linux) 4.8.1 20130909 [gcc-4_8-branch revision 202388 拒绝它:
bug2.cpp:5:1: error: non-constant condition for static assertion
static_assert(&A == &B, "Bug");
^
bug2.cpp:5:1: error: the value of ‘B’ is not usable in a constant expression
bug2.cpp:2:12: note: ‘B’ was not declared ‘constexpr’
const int &B = A;
^
在我看来,GCC 是正确的(而我当然更喜欢 clang 行为)。尝试阅读标准时,我意识到我不足以作为语言律师做出决定。谁能确认一下?
【问题讨论】:
-
clang 3.5 在此代码上生成错误,live。
-
clang项目发展很快,3.3已经过时了。
-
你真的想在某处使用这样的代码吗?只是想知道。
-
当然不是这样。如果您想了解一些背景信息,请参阅我在 stackoverflow.com/questions/22178366/… 上的回答
-
仅供参考,这可能与 C++11 和 C++14 之间
constexpr表达式的规则更改有关; C++14 为constexpr表达式引入了宽松的规则,允许在constexpr函数中改变 状态,因此C++14 中的constexpr并不暗示const(尽管名字很尴尬)。
标签: c++ c++11 language-lawyer constexpr static-assert