https://gcc.gnu.org/bugzilla/show_bug.cgi?id=102995
Bug ID: 102995 Summary: Template friend class declaration of same class with different template parameters fails to allow private methods access Product: gcc Version: 12.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: jdapena at igalia dot com Target Milestone: --- Created attachment 51698 --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=51698&action=edit Test case Declaring a template friend class to the same class fails to resolve the access to private members in its methods. The provided example fails with this error: $ g++ main.cc main.cc: In instantiation of 'bool operator==(const First<A, B>&, const First<C, B>&) [with C = void*; A = int; B = bool]': main.cc:23:15: required from here main.cc:13:61: error: 'int First<A, B>::GetId() const [with A = void*; B = bool]' is private within this context 13 | return lhs.GetId() == rhs.GetId(); | ~~~~~~~~~^~ main.cc:6:9: note: declared private here 6 | int GetId() const { | ^~~~~ The same case works in Clang (verified with Clang 9.0.1). This issue is exposed in Chromium, compiling base/raw_ptr.h unit tests on GCC. I created a simplified version of the problem. See: https://chromium.googlesource.com/chromium/src/base/+/821f1e481ce7c51d31486f26a74d343ffe4cf70e/memory/raw_ptr.h#496 (declaration of the method that should have friend access to rhs private methods). https://chromium.googlesource.com/chromium/src/base/+/821f1e481ce7c51d31486f26a74d343ffe4cf70e/memory/raw_ptr.h#584 (the friend class declaration). https://chromium.googlesource.com/chromium/src/base/+/821f1e481ce7c51d31486f26a74d343ffe4cf70e/memory/raw_ptr_unittest.cc#328 (failing test compilation).