When deriving a class from a template base class, one is forced to use the syntax this->member
, or using Base::member
to get access to a member of the base class. The same would happen if the base class is a generic template parameter and one is sure that some members exists.
Would it be possible to write a macro that "import" all members of the base class that I know (or assume) to be existent?
Simple example:
struct A {
int x = 0;
};
struct B {
int x = 0;
int y = 1;
};
template <class T>
struct C {
int x = 0;
T z = 0;
};
template <class BaseClass>
struct Derived : BaseClass {
using BaseClass::x;
void setx1() { x = 1; }
};
template <class T>
struct Derived_from_C : C<T> {
using C<T>::x;
void setx1() { x = 1; }
};
int main()
{
Derived<A> a;
a.setx1();
Derived<B> b;
b.setx1();
Derived_from_C<double> c;
c.setx1();
return 0;
}
In defining struct Derived
I assume that the template parameter BaseClass
contains a member x
, but to use it in the member setx1
I have to manually declare a using
. The same happens for Derived_from _C
where the base class is a template class. But if the base class contains many members that I would like to use, it becomes tedious and error-prone to add a using
for each member.
It it possible to write a macro that does this semi-automatically? Something like
#define USING(class, __VA_ARGS__)
such that
USING(BaseClass, x, y, z)
expands to
using BaseClass::x;
using BaseClass::y;
using BaseClass::z;