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?
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>
}
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With