以下代码的预期行为是什么?
GCC的输出为0,而clang的输出为1
哪一个是正确的?
#include <iostream>
static const bool ne = false;
struct a
{
a() noexcept(ne) {}
static const bool ne = true;
};
int main()
{
std::cout << noexcept(a()) << std::endl;
}
答案 0 :(得分:4)
他们都是对的!这只是形式错误的代码。来自[basic.class.scope]:
在类S中使用的名称N应在其上下文中引用相同的声明,并在重新评估时引用 S的完整范围违反此规则不需要诊断。
本节还包括以下示例:
[实施例:
... android { ... buildTypes { release { ... minifyEnabled true } } } ...
-end example]
没有全局范围typedef int c;
enum { i = 1 };
class X {
char v[i]; // error: i refers to ::i
// but when reevaluated is X::i
int f() { return sizeof(c); } // OK: X::c
char c;
enum { i = 2 };
};
[...]
,代码是有效的 - 但由于bug 70142(未经证实),gcc无法编译它。