Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using variadic templates to specify friend classes

I'm trying to use variadic templates to specify friend classes. I try with the following syntax, but it doesn't work.

template <class... Args>
struct A {
    friend Args...;
};

I try to code some workarounds, but it seems to be not so simple since the friendship is not transitive and inherited. So the question is if there is a correct syntax or any workaround to make each individual class in Args be a friend of A?

like image 602
mattia.penati Avatar asked Apr 26 '14 02:04

mattia.penati


1 Answers

Maybe the following CRTP variant would be sufficient for your use:

template<typename Arg> class Abase
{
  friend Arg;
  virtual int foo(int) = 0; // this is the private interface you want to access
public:
  virtual ~Abase() {}
};

template<typename... Args> class A:
  public Abase<Args> ...
{
  virtual int foo(int arg) { return frobnicate(arg); }
  // ...
}

Then each class you pass in Args can access that private interface through the corresponding Abase base class, for example

class X
{
public:
  // assumes X is in the Args
  template<typename Args ...> int foo(A<Args...>* p)
  {
    Abase<X>* pX = p; // will fail if X is not in Args
    return pX->foo(3); // works because X is friend of Abase<X>
  }
};
like image 176
celtschk Avatar answered Nov 04 '22 04:11

celtschk