C++ 嵌套名称说明符中的不完整类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24095071/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 00:37:59  来源:igfitidea点击:

Incomplete type in nested name specifier

c++language-lawyer

提问by Filip Roséen - refp

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:

N3797工作草案的3.4.3/1中没有提及:

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

类或命名空间成员或枚举器的名称可以在 :: 作用域解析运算符 (5.1) 之后引用,该运算符应用于表示其类、命名空间或枚举的嵌套名称说明符

So is that behavior implementation dependent?

那么这种行为实现是否依赖?

回答by Filip Roséen - refp

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.2p6Point 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.

3.3.2p6申报点[basic.scope.pdecl]

在类成员声明点之后,可以在其类的范围内查找成员名称。

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 afterit 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.

由于您的前向声明(当然)没有引入任何名为c 的成员,因此引用此类名称是不正确的。



The misleading diagnostic...

误导性的诊断...

The diagnostic issued by both gccand clangwhen being fed your code is somewhat misleading, and honestly I feel a bug report is in order.

gccclang在输入您的代码时发出的诊断信息有些误导,老实说,我觉得应该提交错误报告。

foo.cpp:3:8: error: incomplete type 'A' named in nested name specifier



We areallowed 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
};