c++c++11typesfriendtype-alias

Why is a type alias not allowed to be a friend class name in C++?


class A {    
};

class B {
    using C = A;

    // Compilation error: 
    // Type alias 'C' cannot be referenced with a class specifier 
    friend class C;    
};

Why is a type alias not allowed to be a friend class name in C++?

What's the rationale behind?


Solution

  • Why is a type alias not allowed to be a friend class name in C++?

    You're wrong in assuming that a type alias is not allowed to be in a friend declaration. The correct syntax for befriending through alias C is friend C; instead of friend class C;.


    Note also that the program is ill-formed and msvc is wrong in not giving a diagnostic. This can be seen from dcl.type.elab:

    If the identifier or simple-template-id resolves to a class-name or enum-name, the elaborated-type-specifier introduces it into the declaration the same way a simple-type-specifier introduces its type-name ([dcl.type.simple]). If the identifier or simple-template-id resolves to a typedef-name ([dcl.typedef], [temp.names]), the elaborated-type-specifier is ill-formed.

    (emphasis mine)

    And since the identifier C does resolve to a typedef-name the elaborated-type-specifier(class C) and hence the program is ill-formed.