在数据成员的 qualified-id 之后,有一个引号描述了应用于 const 静态数据成员的数据成员的名称查找:
秒。 3.4.1/13:
A name used in the definition of a static data member of class X (9.4.2) (after the qualified-id of the static member) is looked up as if the name was used in a member function of X.
但是我们可以定义类内静态常量数据成员:
class A
{
static const int i = x;//x is a name defined an arbitrary const int
};
来自秒的规则。 3.4.1/13 不适用于名称 x
查找。 实际应用什么规则?请引用标准中的相应条款。
例如,下面的代码是有效的:
#include <stdio.h>
const int a=5;
class A
{
public:
static const int b=a;//b is unqualified-id of this data-member
};
int main(){ printf("%d\n",A::b); } //5
下面的代码也是有效的:
#include <stdio.h>
class A
{
public:
static const int a=7;
static const int b=a;
};
int main(){ printf("%d\n",A::b); } //7
但以下是无效的:
#include <stdio.h>
class A
{
public:
static const int b=a; //error: a was not declare in this scope.
static const int a=7;
};
int main(){ printf("%d\n",A::b); }
尚不清楚什么规则适用于查找a
。
最佳答案
静态数据成员的类内声明不是定义,即使它包含初始值设定项。因此,§3.4.1/13 不适用于您的示例。
类作用域中的名称查找由哪些作用域到达类作用域的规则定义。 §3.3.7/1.1:
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 (including such things in nested classes).
A name N used in a class S shall refer to the same declaration in its context and when re-evaluated in the completed scope of S. No diagnostic is required for a violation of this rule.
If reordering member declarations in a class yields an alternate valid program under (1) and (2), the program is ill-formed, no diagnostic is required.
这些规则的目的是允许成员函数访问稍后在类中声明的成员变量。因为规则没有提到静态数据成员的初始值设定项,所以静态数据成员的名称查找默认为 3.4.1/7:
A name used in the definition of a class X outside of a member function body, default argument, exception- specification, brace-or-equal-initializer of a non-static data member, or nested class definition shall be declared in one of the following ways:
该列表基本上可以归结为“名称必须是同一类的成员,或者在封闭范围内声明”。在您的第一个示例中, a
是在封闭的命名空间中声明的,因此可以找到它。在第二个示例中,a
在使用之前被声明。
但在第三个示例中,它在声明之前使用,这是非法的 — 如果在封闭的命名空间中声明了某些 a
,则由于 3.3,这将是未定义的行为。 7/1.2.
关于c++ - 什么名称查找规则适用于静态常量数据成员定义中的名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23947055/