使用 'using' 指令隐藏名字

Hiding names with 'using' directive

代码示例:

struct A {};
struct B { using A = A; };

int main()
{
    B b;
}

Clang 编译它。但是 GCC 给出以下错误 (demo):

declaration of 'using A = struct A' changes meaning of 'A'

C++ standard 说:

If a class name ([class.name]) or enumeration name ([dcl.enum]) and a variable, data member, function, or enumerator are declared in the same declarative region (in any order) with the same name (excluding declarations made visible via using-directives ([basic.lookup.unqual])), the class or enumeration name is hidden wherever the variable, data member, function, or enumerator name is visible.

UPD.0:感谢来自莫斯科的 Vlad

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

这是否意味着 GCC 行为不正确?谢谢!

看来是gcc的bug。根据 C++ 20 标准(6.3.7 Class 范围)

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

在这种情况下

struct A {};
struct B { using A = A; };

名称 B::A 指的是 struct A 的相同声明。

这是 C++ 标准中的一个示例,它显示了引用的含义。

typedef char* T;
struct Y {
   T a; // error: T refers to ::T but when reevaluated is Y::T
   typedef long T;
   T b;
};