Friend member function compiles in GCC but not in Clang or MSVC

139 Views Asked by At

I have been working with the following program that compiles with GCC, but is rejected by both Clang and MSVC.

Godbolt Demo link

template <typename>
class X {
   
    void f(){}
};
class Y
{
    friend void X<int>::f();  
};

int main()
{
    X<int> t;
    t.f();
    Y b;
}

As you can see the above program works with GCC, but Clang says:

<source>:9:25: error: friend function 'f' is a private member of 'X<int>'
    friend void X<int>::f();  
                ~~~~~~~~^
<source>:9:25: note: implicitly declared private here
<source>:15:7: error: 'f' is a private member of 'X<int>'
    t.f();
      ^
<source>:9:25: note: implicitly declared private here
    friend void X<int>::f();  
                        ^
2 errors generated.
Compiler returned: 1

I want to know what the correct behavior of the program is; which compiler is correct according to the standard? I am using C++20.

1

There are 1 best solutions below

0
user12002570 On

The program is ill-formed because the qualified-name X<int>::f is not accessible inside the class Y that contains the friend declaration. This can be seen from class.friend:

A name nominated by a friend declaration shall be accessible in the scope of the class containing the friend declaration. The meaning of the friend declaration is the same whether the friend declaration appears in the private, protected, or public ([class.mem]) portion of the class member-specification.

(emphasis mine)

And since the name X<int>::f nominated by the friend declaration friend void X<int>::f(); is not accessible inside the containing class Y, the program is ill-formed and GCC is wrong in accepting the program.


Here is the corresponding GCC bug:

GCC accepts invalid inaccessible friend declaration of member function