c++ - 在限定的声明符 id 之后进行名称查找

标签 c++ static-members qualified-name

我试图理解以下引用的含义(3.4.3/3 N3797):

names following the qualified-id are looked up in the scope of the member’s class or namespace.

namespace A
{
    class C
    {
    public:
        static const int a=7;
        static int b;
    };
}

int A::C::b=a; //7

static int b; 的范围b 声明点后面的声明区域组成。实际上:

The potential scope of a name declared in a class consists not only of the declarative region following the name’s point of declaration, but also of all function bodies, default arguments, exception-specifications, and brace-or-equal-initializers of non-static data members in that class

这意味着static const int a=7;不属于static int b;的范围。因此,在int A::C::b=a;中找不到static const int a=7

这是标准中的拼写错误还是我的误解?

最佳答案

This implies that static const int a=7; does not belong to the scope of static int b;. Hence the static const int a=7 cannot be found in the int A::C::b=a;.

没有。它准确地暗示了您可以在那里读到的内容:在类中声明的名称的潜在范围包含非静态数据成员的函数体等。这与上面的引用并不冲突 - 静态数据成员的声明区域(和范围)仍然包含它本身声明的类的范围。

您自己引用了相关部分:

names following the qualified-id are looked up in the scope of the member’s class or namespace

因此,由于在此代码片段中

int A::C::b=a;

a 用在 declarator-id 之后,在类中查找并找到。

关于c++ - 在限定的声明符 id 之后进行名称查找,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24038003/

相关文章:

c++:流式传输和速率调节

c++ - 一轮编译生成两个或多个输出文件

c++ - 在类嵌套静态 const 成员变量初始化 Clang vs GCC 哪个编译器是正确的?

haskell - 如何在 Haskell 中编写符号的限定名称?

c++ - 使用 using 指令进行不明确的名称查找

c++ - 由于标准测试失败,用于查找点是否在 2D 多边形内的替代测试

c++ - 在 VS 2010 C++ 中编译项目时出错

c++ - 静态字段初始化的模板部分特化

c++ - 错误 LNK2001 : unresolved external symbol public: static class

python - 使用元素树解析 wsdl(从定义中检索 namespace )