Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not able to befriend typedefs: any particular reason?

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.

like image 701
Lightness Races in Orbit Avatar asked May 13 '11 15:05

Lightness Races in Orbit


1 Answers

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
};
like image 128
Johannes Schaub - litb Avatar answered Oct 31 '22 19:10

Johannes Schaub - litb