10
struct A {};
typedef A B;

struct C { friend struct B; };

GCC 4.7.0 20110427 tells me error: using typedef-name 'B' after 'struct'.

So far, this seems pretty self-explanatory; after all, my example code is trying to declare-and-friend a struct called B, which is in fact not a struct-key.

However, I have to write friend struct A; if A is in fact a complex, long-winded mess of template metahackery, this is not desirable.

Am I missing something, or can we in fact not friend types through type aliases? If not, is there any particular reason or is it just a quirk of the language?


This question brought up the issue before, but is dated and makes assertions on the matter regarding C++0x that don't appear to be true. This question instead regards the C++0x FDIS.

Community
  • 1
  • 1
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055

1 Answers1

11

You can befriend arbitrary types (for non-class types, the friend declaration will be ignored), but then you shall omit struct:

struct A {};
typedef A B;

struct C { 
  friend B; // equivalent: friend struct A;
            // equivalent: friend A;

  friend int; // ignored
};
Johannes Schaub - litb
  • 496,577
  • 130
  • 894
  • 1,212