Repository navigation
A friend function defined in a befriended class cannot access the granting class's private members #320
Saldivarcher
started this conversation in
Issue Triage - Compilation
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Issue Summary
A friend function that is defined inside a befriended class does not get that
class's access. Class
Xdeclares classYa friend. A hidden friendfunction that
Ydefines cannot name a private member ofX. The front endrejects the access in the function body. In a trailing return type, the access
failure removes the candidate during deduction, and overload resolution then
finds no match.
In the reproducer, each
__wrap_iter<T>befriends every__wrap_iter<U>.The
operator-that__wrap_iter<const char *>defines reads__wrap_iter<char *>::__i_.Reproducing Source Code
https://godbolt.org/z/9G3GK5n66
Command-line Options
cpfe --gnu_version=150301 --c++20 --no_wrap_diagnostics --no_code_gen friend_access.cpp
Type of Issue
[Front End] This code SHOULD compile with these options.
Additional Details
Output:
--clang_version=210000and the default mode give the same error. EDG 6.5,7.0 and trunk on Compiler Explorer give the same error too.
Expected: no diagnostic. [class.friend]/2 lets a befriended class name
private members of the granting class in its member declarations. The friend
definition of
operator-is a member-declaration of__wrap_iter<const char *>. GCC 15 and Clang 21 accept the program at-std=c++20.Analysis
The access check is the cause:
When
__i_is public, the front end accepts the program.When the trailing return type is removed, the front end reports the access
failure in the body directly:
MSVC also rejects the program (
error C2676, with the note'std::__wrap_iter<char *>::__i_': cannot access private member).CWG1699 ("Does befriending
a class befriend its friends?") is open. Thus the strict conformance answer is
not settled. But GCC and Clang both accept the program. Thus the front end
should accept it, at least in the GNU and Clang emulation modes.
Possibly related: #158 (GNATS 27807) reports a spurious "is inaccessible"
error in Boost's
rbtree_best_fit.hpp. That report also names a friend. I didnot examine the Boost code, so I do not know if the cause is the same.
Reproducing (Standard) Configuration(s)
linux-gcc-debug
Reproducing (Non-standard) Configuration
No response
I acknowledge that:
LICENSE.txtfile in my.zipor.tar.gzupload. If I included aLICENSE.txtfile, any tests created using the reproducing source code will be governed by the terms of the license I provided instead.All reactions