typedef changes meaning

前端 未结 2 869
失恋的感觉
失恋的感觉 2020-11-29 10:07

When I compile the following snippet with g++

template
class A
{};

template
class B
{
    public:
        typedef         


        
2条回答
  •  攒了一身酷
    2020-11-29 10:51

    g++ is correct and conforming to the standard. From [3.3.7/1]:

    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.

    Before the typedef, A referred to the ::A, however by using the typedef, you now make A refer to the typedef which is prohibited. However, since no diagnostic is required, clang is also standard conforming.

    jogojapan's comment explains the reason for this rule. Take the following change to your code:

    template
    class A
    {};
    
    template
    class B
    {
        public:
            A a; // <-- What "A" is this referring to?
            typedef     A            A;
    };
    

    Because of how class scope works, A a; becomes ambiguous.

提交回复
热议问题