Hiding names with 'using' directive

可紊 提交于 2021-02-09 11:10:20

问题


Code example:

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

int main()
{
    B b;
}

Clang compiles it. But GCC gives the following error (demo):

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

The C++ standard says:

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: thanks to Vlad from Moscow

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

Does that mean that GCC behavior is incorrect? Thanks!


回答1:


It seems that it is a bug of gcc. According to the C++ 20 Standard (6.3.7 Class scope)

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.

In this case

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

the name B::A refers to the same declaration of struct A.

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


来源:https://stackoverflow.com/questions/61455507/hiding-names-with-using-directive

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!