A namespace that has the same name as the containing class, ok for gcc, is not suitable for clang

Consider the code:

template<typename T> 
class Foo{};

namespace X
{
    class X{};
}

using namespace X; // now both class X and namespace X are visible
Foo<X::X> f()
{
    return {};
}

int main() {}

gcc5.2 compiles the code without any errors. However, clang spits out the error:

error: "X" is not a class, namespace or enumeration Foo f ()

error: reference to "X" is ambiguous

Is the code syntactically valid according to the C ++ standard? Or is it just a gcc error? Removing a qualified name X::Xand using it Foo<X>instead makes the gcc choke also

error: template argument 1 is not valid Foo f ()

+4
source share
2 answers

[namespace.udir] / 6:

, .

X X::X , . , X; , , . ( X, ).

::X . Lookup . [Namespace.qual]/2:

X m S (X, m) : S 0 (X, m) - m X X (7.3.1). S 0 (X, m) , S (X, m) S 0 (X, m); [...]

X - , m - "X". , , .

+2

, , , , .

#include <iostream>
#include <string>

namespace test
{
    void function1(void){std::cout << "Function inside the namespace" << std::endl;}

    class test
    {
    public:
        static void function1(void){std::cout << "Function inside the class" << std::endl;}    
    };
};

using namespace test;

int main()
{
    function1();

    test::function1();

    test::test::function1();
}

(GCC 4.9.2)

"Function inside the namespace"
"Function inside the namespace"
"Function inside the class"
+1

Source: https://habr.com/ru/post/1612991/


All Articles