I have been working with the following program that compiles with GCC, but is rejected by both Clang and MSVC.
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.
The program is ill-formed because the qualified-name
X<int>::fis not accessible inside the classYthat contains the friend declaration. This can be seen from class.friend:(emphasis mine)
And since the name
X<int>::fnominated by the friend declarationfriend void X<int>::f();is not accessible inside the containing classY, 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