Quuxplusone / LLVMBugzillaTest

0 stars 0 forks source link

[cwg1699] clang interprets friendship rules lexically; other compilers do not #43678

Open Quuxplusone opened 4 years ago

Quuxplusone commented 4 years ago
Bugzilla Link PR44708
Status NEW
Importance P enhancement
Reported by Grzegorz Jablonski (grzejabl@gmail.com)
Reported on 2020-01-29 08:50:32 -0800
Last modified on 2020-01-30 19:27:33 -0800
Version 9.0
Hardware PC Linux
CC blitzrakete@gmail.com, dblaikie@gmail.com, dgregor@apple.com, erik.pilkington@gmail.com, llvm-bugs@lists.llvm.org, richard-llvm@metafoo.co.uk
Fixed by commit(s)
Attachments
Blocks
Blocked by
See also
The following code:

class A
{
  friend class B;
  int m;
};

class B
{
  friend void f(A* p) { p->m = 32; }
};

compiles in clang version 9.0.0-2. If the function body is defined outside of
the class, the compiler correctly issues an error about not being able to
access the private member.
Quuxplusone commented 4 years ago

Well, per [class.access.base]p5, "A member m is accessible at the point R when named in class N if [...] m as a member of N is private, and R occurs in a [...] friend of class N"

The reference to p->m occurs in class B, which is a friend of class A, so arguably this is valid.

This question is the subject of CWG issue 1699 (http://wg21.link/cwg1699), and the consensus opinion when that was last discussed seemed to be that Clang's current behavior is correct.