我试图理解以下引用的含义(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/