C++:在初始化程序中类静态变量的定义中查找名称

标签 c++ c++11 static-members decltype static-assert

我有简单的代码:

#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/

相关文章:

c++ - 我可以在单读/单写队列中用 volatile 替换原子吗?

c++ - 从魔数(Magic Number)到 int 或 long 的重载解析(在 range-v3 中)

c++ - Auto 和 Void 的区别?

vb.net - 延迟实例化静态类的成员是否有益?

Swift 结构属性与结构本身的类型相同。递归?

cpp 文件中的 C++ 值定义和额外限定

c++ - 为什么会出现符号查找错误?

c++ - 按值返回的函数无法按预期工作

c++ - shared_ptr - ccomptr 交换

c++ - 对 std::regex_match(和 std::regex_search)参数生命周期的约束