Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 375
xmllmx Avatar asked Dec 06 '25 07:12

xmllmx


1 Answers

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.

like image 73
Anoop Rana Avatar answered Dec 09 '25 17:12

Anoop Rana



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!