我有简单的代码:
#include <type_traits>
class A {
public:
static int a;
};
void a() {}
int A::a = [](){static_assert(std::is_function<decltype(a)>::value,"'a' is not a function");return 777;}();
int main() {
return 0;
}
在编译期间(使用 g++ 4.8.1 和 clang 3.4)关于 'a' 的 get static 断言错误不是函数。但是在 assert 内部,在 decltype 中我输入了 'a'(它是一个函数)而不是 A::a。 编译器不应该采用函数 (a) 而不是类成员 (A::a) 吗?
您能否在解释 C++ 规范的地方给出任何引用?
最佳答案
Shouldn’t compiler took a function (a) instead a class member (A::a)?
没有;成员定义在类范围内,因此 a
的非限定查找给出了成员。
Can you give any reference to C++ specification where it is explained?
类作用域在C++11 3.3.7中定义;特别是:
The potential scope of a declaration that extends to or past the end of a class definition also extends to the regions defined by its member definitions, even if the members are defined lexically outside the class (this includes static data member definitions)
关于C++:在初始化程序中类静态变量的定义中查找名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22784926/