Class declaration in same scope as using declaration compiles in GCC but not MSVS

后端 未结 2 1642
逝去的感伤
逝去的感伤 2020-12-18 07:25

Is the following program well-formed according to the c++ standard?

namespace X { class A; }

namespace Y { using X::A; class A {}; }

int main() {}
<         


        
2条回答
  •  萌比男神i
    2020-12-18 08:12

    I believe the program is ill formed. [basic.scope.declarative]/4 says:

    Given a set of declarations in a single declarative region, each of which specifies the same unqualified name,

    — they shall all refer to the same entity, or all refer to functions and function templates; or

    — exactly one declaration shall declare a class name or enumeration name that is not a typedef name and the other declarations shall all refer to the same variable or enumerator, or all refer to functions and function templates; in this case the class name or enumeration name is hidden

    The two declarations of unqualified name A refer to different entities, both of which are classes.

    (Interestingly, neither GCC 6.0 nor Clang 3.7 seem to diagnose it that way. Both accept the code as written (not diagnosing the declaration of two distinct classes with the same name). If you add X::A a; to the body of main, then Clang complains about the incomplete type of X::A.)

提交回复
热议问题