Program with chaining of using-declarations compiles on MSVS and clang but not on GCC

前端 未结 2 1943
灰色年华
灰色年华 2021-02-07 04:37

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

namespace X { int i; }

namespace Y { using X::i; }

int main() { using X::i; u         


        
2条回答
  •  终归单人心
    2021-02-07 05:18

    Clang and MSVC are correct; this code is valid. As Alf notes, [namespace.udecl] (7.3.3)/10 says

    A using-declaration is a declaration and can therefore be used repeatedly where (and only where) multiple declarations are allowed.

    However, there is no restriction on multiple declarations of the same entity in block scope, so the original example is valid. A corresponding case not involving using-declarations is:

    int n;
    void f() {
      extern int n;
      extern int n;
    }
    

    This is valid (and is accepted by GCC, EDG, Clang, and MSVC), therefore (by the above-quoted rule) the original example is also valid.

    It is worth noting that the example in [namespace.udecl] (7.3.3)/10 contains an error. It says:

    namespace A {
      int i;
    }
    
    void f() {
      using A::i;
      using A::i; // error: double declaration
    }
    

    ... but the comment is not correct; there is no error on the second declaration. See the discussion in core issue 36. I've removed the example from the standard so that it won't confuse more people.

提交回复
热议问题