使用"using"指令隐藏名称 [英] Hiding names with 'using' directive
问题描述
代码示例:
struct A {};
struct B { using A = A; };
int main()
{
B b;
}
Clang编译它.但是GCC给出了以下错误(演示):
Clang compiles it. But GCC gives the following error (demo):
声明使用A =结构A"会更改"A"的含义
declaration of 'using A = struct A' changes meaning of 'A'
C ++标准说:
如果在同一声明性区域(以任何顺序)中声明了类名([class.name])或枚举名([dcl.enum])以及变量,数据成员,函数或枚举器具有相同的名称(不包括通过使用指令可见的声明([basic.lookup.unqual])),无论变量,数据成员,函数或枚举器名称在何处,类或枚举名称均隐藏可见.
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 :感谢莫斯科的弗拉德
UPD.0: thanks to Vlad from Moscow
在S类中使用的名称N在上下文中以及在S的完整范围中进行重新评估时应引用同一声明.对于此规则的违反,无需进行诊断
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行为不正确?谢谢!
Does that mean that GCC behavior is incorrect? Thanks!
推荐答案
似乎是gcc的错误.根据C ++ 20标准(6.3.7类范围)
It seems that it is a bug of gcc. According to the C++ 20 Standard (6.3.7 Class scope)
2在S类中使用的名称N应指代与它的上下文以及在S的完整范围中进行重新评估时.违反此规则需要进行诊断.
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
相同的声明.
the name B::A
refers to the same declaration of struct A
.
这是C ++标准中的一个示例,它显示了引号的含义.
Here is an example from the C++ Standard that shows the meaning of the quote.
typedef char* T;
struct Y {
T a; // error: T refers to ::T but when reevaluated is Y::T
typedef long T;
T b;
};
这篇关于使用"using"指令隐藏名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!