我尝试在嵌套名称说明符中使用不完整类型,如下所示:
class A;
int b= A::c; // error: incomplete type ‘A’ used in nested name specifier
class A {
static const int c=5;
};
字符串
在N3797工作草案的3.4.3/1中对此没有任何说明:
类或命名空间成员或枚举数的名称可以在::scope解析运算符(5.1)之后引用,该运算符应用于表示其类、命名空间或枚举的nested-name-specifier
那么这个行为依赖于实现吗?
2条答案
按热度按时间kx1ctssn1#
简介
标准中有几个地方暗示你的代码是格式错误的,但下面的引用本身就说明了这一点:
3.3.2p6
声明点[basic.scope.pdecl]
在声明类成员之后,可以在其类的作用域中查找成员名称。
你的代码的问题不在于你试图到达一个不完整类型的主体内部,问题在于你只能在声明了一个类成员名**之后才能引用它。
因为你的前向声明(当然)没有引入任何名为 c 的成员,引用这样的名字是不正确的。
误导性诊断...
由 gcc 和 clang 发布的诊断有点误导,老实说,我觉得一个bug报告是正确的。
字符串
我们被允许在 nested-name-specifier 中命名一个不完整的类型,但正如前面所说的,我们不允许引用一个尚未声明的成员。
型
型
uz75evzq2#
当我访问一个类的成员变量时,我得到了同样的错误,这个类在头文件中被前向声明,但没有包含在我访问它的main文件中。
myClass.h
字符串
main.cpp
型