Bug 29615 - Class can't be friend of itself?
Summary: Class can't be friend of itself?
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.2.0
: P3 minor
Target Milestone: 4.4.0
Assignee: Not yet assigned to anyone
URL:
Keywords: diagnostic
Depends on:
Blocks:
 
Reported: 2006-10-27 14:05 UTC by Danny Boelens
Modified: 2012-01-05 14:01 UTC (History)
3 users (show)

See Also:
Host: i686-pc-linux-gnu
Target: i686-pc-linux-gnu
Build: i686-pc-linux-gnu
Known to work: 4.4.6, 4.6.3, 4.7.0
Known to fail:
Last reconfirmed: 2006-10-27 22:57:33


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Danny Boelens 2006-10-27 14:05:56 UTC
The following simple C++ file:

<test.cpp>
class A
{
  friend class A;
};

int main() { return 0; }
</test.cpp>

results in the following error when I try to compile it with g++:

$ ~/gcc-build-4.2-20061007/gcc/g++ -o test test.cpp
test.cpp:3: error: class 'A' is implicitly friends with itself

I was using a snapshot (GCC 4.2 2006/10/07), but I got the same behaviour with several 4.1.x and 4.0.x builds on both Linux and Windows 2000 (Cygwin).

I agree that it's pointless to make a class a friend of itself (which is why I used severity 'minor'), but I haven't found anything in my C++ spec (ISO/IEC 14882) that say it is not allowed. Please point my to the relevant section in case I missed anything.

By the way: all other compilers I've tried accept this file without a problem. These include Visual Studio .NET 2003, CodeWarrior 8.3, CodeWarrior 9.6 and the online test drive for Comeau. Comeau does exactly what I'd expect:

Your Comeau C/C++ test results are as follows: 

Comeau C/C++ 4.3.8 (Aug 19 2006 13:36:48) for  ONLINE_EVALUATION_Alpha1
Copyright 1988-2006 Comeau Computing.  All rights reserved.
MODE:strict errors C++

"ComeauTest.c", line 3: warning: pointless friend declaration
       friend class A;
       ^

Not a big issue, but I thought it might be a good idea to log this anyway, since a search didn't result in relevant hits :-)
Comment 1 Wolfgang Bangerth 2006-10-27 22:57:33 UTC
Confirmed.
Comment 2 Andrew Pinski 2006-10-28 18:44:27 UTC
As far as I can tell this has been a pedwarning since the begining of the C++ front-end.
Comment 3 jens-devel@gmx.de 2007-10-15 12:48:38 UTC
Subject:  Class can't be friends of itself?

Hi,

I think gcc should be able to compile something like this.
There is no other way to make a member-variable accessible only from all
objects which are of the same type. Am I wrong?

<test.cpp>
class A
{
  friend class A;

  private:
	int var_accessible_from_all_A_objects;
};

int main() { return 0; }
</test.cpp>

Greetings
Jens

Comment 4 David Krauss 2010-03-26 21:08:43 UTC
The error doesn't occur if the friend is a template instance, so it really doesn't hurt anyone. But it's weird.

template< int z >
class F {
    friend class F<0>; // error only if <0> removed
};

template class F<0>;
Comment 5 Jonathan Wakely 2010-03-27 20:14:59 UTC
With current versions this is only a warning not an error, changing keywords from rejects-valid to diagnostic

(In reply to comment #3)
> There is no other way to make a member-variable accessible only from all
> objects which are of the same type. Am I wrong?

Yes.

(In reply to comment #4)
> The error doesn't occur if the friend is a template instance, so it really
> doesn't hurt anyone. But it's weird.
> 
> template< int z >
> class F {
>     friend class F<0>; // error only if <0> removed
> };

Well in that case it isn't friends with itself, so not really weird.

This shows that being a template has nothing to do with it:

template< int z >
class F {
    friend class F<z>;
};

warning: class ‘F<z>’ is implicitly friends with itself
Comment 6 Jonathan Wakely 2010-03-27 20:19:27 UTC
I think the warning is reasonable, but it would be nice if it could be disabled.
Comment 7 Paolo Carlini 2012-01-05 00:29:07 UTC
Jason, I think we are close to resolving this issue: can you confirm that the warning we are currently emitting is ok? In case, shall we make possible disabling it? Thanks!
Comment 8 Jason Merrill 2012-01-05 05:43:22 UTC
The submitter said that he expected a warning, and it seems correct to me.  It would probably be good to have a general mechanism for disabling individual warnings by code number, but apart from that I don't see any need to be able to disable this one.
Comment 9 Paolo Carlini 2012-01-05 07:42:01 UTC
Fair enough, let's close this, then.
Comment 10 Jason Merrill 2012-01-05 14:01:02 UTC
This seems to have been fixed in 4.4.