[cfe-dev] Three code snippets accepted by clang and rejected by g++

John McCall rjmccall at apple.com
Mon Aug 11 23:24:16 PDT 2014


On Aug 11, 2014, at 2:49 PM, Richard Smith <richard at metafoo.co.uk> wrote:
> On Sun, Aug 10, 2014 at 7:10 AM, Nicola Gigante <nicola.gigante at gmail.com> wrote:
> Hello.
> 
> While testing a recently written piece of code to see if it worked on linux
> I found three differences between clang and g++ that make the first
> to compile the code and the second to reject it. I'm talking about the
> clang shipped with Xcode 5.1.1, I don't have a working trunk and my
> machine is slow to compile, sorry.
> 
> Below the three issues:
> 
> 1) transitive-friend.cpp
> class A {
>   friend class B;
>   int v;
> };
> 
> struct B {
>   A a;
>   friend int f(B b) {
>      return b.a.v;
>   }
> };
> 
> Clang accept this code because
> I access the private field of A in the scope of B
> and B is a friend. g++ rejects it:
> 
> $ g++-4.9 -std=c++11 -fsyntax-only differences.cpp
> differences.cpp: In function 'int f(B)':
> differences.cpp:3:7: error: 'int A::v' is private
>    int v;
>        ^
> differences.cpp:9:17: error: within this context
>       return b.a.v;
>                  ^
> 
> Clang's behavior reflects the current intent of the C++ core working group. This is core issue 1699, and the current resolution to that is to treat everything that is lexically within a befriended entity as being befriended. GCC does not yet implement the resolution to core issue 1699 (which is reasonable, since it's not yet even resolved).

Great, this is the best resolution.  There are many unfortunate corner cases not caught by the current wording, including nested classes within inline friend functions.

John.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20140811/eaace6bf/attachment.html>


More information about the cfe-dev mailing list