嵌套名称说明符中的不完整类型 [英] Incomplete type in nested name specifier
问题描述
我试图在嵌套名称说明符中使用不完整类型,如下所示:
int b = A :: c; //错误:不完整类型'A'用于嵌套名称说明符
类A {
static const int c = 5;
};
在N3797工作草案的3.4.3 / 1中没有提到:
类或命名空间成员或枚举器的名称可以引用
到:: scope解析运算符(5.1)应用于
表示其类,命名空间或
的枚举的嵌套名称说明符
在标准中有多个地方?
隐含意味着你的代码是不成形的,但下面的引号说明了自己:
3.3.2p6
声明点[basic.scope.pdecl]
< blockquote>
在成员声明之后,成员名可以在其类的范围内查找。
你的代码的问题不是你试图到达一个不完整类型的主体内部,问题是你只能引用一个类成员名称因为你的前向声明(当然)没有引入任何名为 c 的成员,
令人误解的诊断...
在喂食你的代码时, gcc 和 clang 发出的诊断有些误导,老实说我觉得一个错误报告是有序的。
foo.cpp:3:8:error:incomplete类型'A'名称说明符
我们 允许在 nested-name-specifier 中命名不完整的类型,但如上所述;我们不允许参考尚未宣布的成员。
形式不正确:
class X {
static int a [X :: x]; //未形成,`X :: x`尚未声明
static int const x = 123;
};
legal:
class X {
int const x = 123;
int a [X :: x]; // legal,`X`是不完整的(因为我们仍然定义它)
//但我们仍然可以引用一个_declared_成员
};
I tried to use incomplete type in nested name specifier as the following:
class A;
int b= A::c; // error: incomplete type ‘A’ used in nested name specifier
class A {
static const int c=5;
};
There is says nothing about it in the 3.4.3/1 of N3797 working draft:
The name of a class or namespace member or enumerator can be referred to after the :: scope resolution operator (5.1) applied to a nested-name-specifier that denotes its class, namespace, or enumeration
So is that behavior implementation dependent?
Introduction
There are several places in the standard that implicitly implies that your code is ill-formed, but the below quotation speaks for itself:
3.3.2p6
Point of declaration[basic.scope.pdecl]
After the point of declaration of a class member, the member name can be looked up in the scope of its class.
The problem with your code isn't that you try to reach inside the body of an incomplete type, the problem is that you can only refer to a class member name after it has been declared.
Since your forward-declaration (of course) doesn't introduce any member named c, it is ill-formed to refer to such name.
The misleading diagnostic...
The diagnostic issued by both gcc and clang when being fed your code is somewhat misleading, and honestly I feel a bug report is in order.
foo.cpp:3:8: error: incomplete type 'A' named in nested name specifier
We are allowed to name an incomplete type in a nested-name-specifier, but as said; we are not allowed to refer to a member that has not yet been declared.
ill-formed:
class X {
static int a[X::x]; // ill-formed, `X::x` has not yet been declared
static int const x = 123;
};
legal:
class X {
int const x = 123;
int a[X::x]; // legal, `X` is incomplete (since we are still defining it)
// but we can still refer to a _declared_ member of it
};
这篇关于嵌套名称说明符中的不完整类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!