3

I have a class (User in the example below) that derives from a base class (Base), passing a third class type (Used) and a member pointer within that class as template parameters to the base class.

What do I need to do within Used (i.e., what do I need to befriend) such that the pointed to data member can be private?

class User;

template <typename T, int T::*member>
class Base {

};

class Used {
    // Befriend everything there is.
    friend class User;
    template <typename T, int T::*member> friend class Base;
    // The variable that should be accessible.
    int i;
};

// error: ‘int Used::i’ is private
class User : public Base<Used, &Used::i> {

};

(Not) working example on Ideone

Edit: Given that this seems to work on newer versions of GCC, how can I work around the problem given g++ 4.8.4?

zennehoy
  • 6,405
  • 28
  • 55
  • 1
    [Works just fine on a more recent version of GCC and on Clang](http://coliru.stacked-crooked.com/a/303ed2b10e4378f5). [Just befriending `User` is sufficient](http://coliru.stacked-crooked.com/a/a955b9ddf9030b29). – T.C. Dec 22 '15 at 16:06
  • Works for me with g++ 4.9.3. – R Sahu Dec 22 '15 at 16:15
  • 1
    Looks like a manifestation of https://gcc.gnu.org/bugzilla/show_bug.cgi?id=59482. – T.C. Dec 22 '15 at 16:15
  • @T.C. Thanks for the link, that looks very similar to the problem indeed. Any ideas for a workaround that does not involve making `i` public? – zennehoy Dec 22 '15 at 16:18
  • Something like [this](http://melpon.org/wandbox/permlink/ziICOE8RtNJAYq5s)? – T.C. Dec 22 '15 at 16:20
  • @T.C. Looks promising, I'll give it a try. Thanks! – zennehoy Dec 22 '15 at 16:25

0 Answers0