Suppose I have a template class, which depending on a template parameter, may or may not have a member int x
.
This can be realized by inheriting from a base template class, which for some specialization has a member int x
.
Example code:
#include <iostream>
template <bool present>
struct base;
template <>
struct base<true> { int x; };
template <bool present>
struct base { };
template <bool activate>
struct A : public base<activate> {
void print() const;
};
template <bool activate>
void A<activate>::print() const
{
if constexpr (activate) {
std::cout << "x = " << this->x << std::endl;
} else {
std::cout << "nothing" << std::endl;
}
}
int main()
{
A<true> a;
a.print();
A<false> b;
b.print();
return 0;
}
In the code above A<true>
contains a member int x
, inherited from base<true>
, whereas A<false>
does not contain it.
Now, since x
is a dependent name, in order to access it, I need to use this->x
or base<true>::x
. This can be somewhat burdersome to use it every time, so the common solution is to employ a using
directive like
using base<true>::x;
inside the definition of A
. But, of course, this makes sense only when activate=true
.
Is it possible, perhaps with a macro, to add using base<true>::x
in the definition of A
, only when a condition (here activate=true
) is satisfied?