Bug 21498 - [c++0x] friend declaration can name non-class with class-key
Summary: [c++0x] friend declaration can name non-class with class-key
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.0.0
: P2 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: accepts-invalid
: 22047 23385 28895 29123 (view as bug list)
Depends on:
Blocks: c++-friends, cxxfriends
  Show dependency treegraph
 
Reported: 2005-05-10 16:17 UTC by Kai-Uwe Bux
Modified: 2019-01-27 05:47 UTC (History)
8 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail: 2.95.3, 3.0.4, 3.2.3, 3.3.3, 3.4.0, 4.0.0, 4.1.0
Last reconfirmed: 2019-01-24 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Kai-Uwe Bux 2005-05-10 16:17:56 UTC
The following code should be rejected, but is accepted by g++:


template < typename T >
class identity {
public:

  typedef T me;

};

template < typename T >
class my_friend {
private:

  friend class identity< T >::me;

  char x;

};

class The_T {
public:

  static
  char & peek_friend ( my_friend< The_T > & f ) {
    return( f.x );
  }
  
};

int main (void) {
  my_friend< The_T > x;
}


The configuration is:

gcc> g++ -v
Using built-in specs.
Target: i686-pc-linux-gnu
Configured with: ../gcc-4.0.0/configure --prefix=/pkg/gcc-4.0.0/usr
Thread model: posix
gcc version 4.0.0


The code is illegal because the friend declaration resolves into the
typedef "me" (7.1.5.3/2).

PS: I do not really understand why the standard is worded this way.
    It appears that templated friendship could be good.
Comment 1 Andrew Pinski 2005-05-10 16:31:02 UTC
Confirmed.
Comment 2 Harald van Dijk 2005-08-14 16:48:10 UTC
*** Bug 23385 has been marked as a duplicate of this bug. ***
Comment 3 David Fang 2006-07-01 01:06:26 UTC
Tripped over this bug while testing some code against ICPC... almost filed a dupe!  
Using the test case from PR 23385, still reproducible (accepts-invalid) on mainline g++ 4.2.0-20060610.

I'd like to understand the rationale behind this C++ standard clause...  
Comment 4 Steven Bosscher 2006-07-01 11:18:03 UTC
Mark, this bug concerns a C++ standard question, perhaps you could give your interpretation...
Comment 5 Mark Mitchell 2006-07-01 22:09:28 UTC
This code is not invalid, and G++ is correct to accept it.

DR68 permits this grammatical production and typedef-names for classes are class-names.
Comment 6 Harald van Dijk 2006-07-02 08:44:32 UTC
> This code is not invalid, and G++ is correct to accept it.
> 
> DR68 permits this grammatical production and typedef-names for classes are
> class-names.

Does that mean this code is valid, too?

typedef class A {} a;
class B {
    friend class a;
};

GCC 4.1.1 rejects it with error: using typedef-name &#8216;a&#8217; after &#8216;class&#8217;.

And if it is valid, should a new bug be opened for it?
Comment 7 David Fang 2006-07-02 17:37:01 UTC
Subject: Re:  clause 7.1.5.3/2 of the c++ is not enforced

Would it be appropriate to prefix [DR 68] to the subject?

Comment 8 Mark Mitchell 2006-07-03 05:19:20 UTC
I'm sorry; I had missed the fact that the elaborated type specifier refers uses a typedef-name.  This code is in fact invalid, since an elaborated type specifier may not refer to a typedef-name.
Comment 9 Pawel Sikora 2006-09-05 10:20:56 UTC
*** Bug 28895 has been marked as a duplicate of this bug. ***
Comment 11 Andrew Pinski 2006-09-18 23:42:33 UTC
*** Bug 22047 has been marked as a duplicate of this bug. ***
Comment 12 Andrew Pinski 2006-09-18 23:42:47 UTC
*** Bug 29123 has been marked as a duplicate of this bug. ***
Comment 13 Andrew Pinski 2006-09-19 05:18:18 UTC
I don't know the state of this feature in C++0x at all and I don't know if this means this is now valid code but only with an option like -std=c++0x.
Comment 14 Jason Merrill 2009-11-03 18:40:35 UTC
In C++0x the testcase is still ill-formed, but changing "friend class" to "friend typename" makes it well-formed.  I'm not going to give an error for this usage until the other usage is implemented.

http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2005/n1791.pdf

Comment 15 Jonathan Wakely 2011-09-10 00:03:36 UTC
(In reply to comment #14)
> In C++0x the testcase is still ill-formed, but changing "friend class" to
> "friend typename" makes it well-formed.  I'm not going to give an error for
> this usage until the other usage is implemented.
> 
> http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2005/n1791.pdf

... which you did for PR 47721 :)