Given this code:
template < int I >
class Foo
{
public:
int v;
Foo() { v = I; }
virtual ~Foo() {}
};
class Bar : public Foo<0>, public Foo<3>
{
public:
template < int I >
int getValue() { return Foo<I>::v; }
};
int main() {
Bar b;
cout << b.getValue<0>() << endl; // prints 0
cout << b.getValue<3>() << endl; // prints 3
cout << b.getValue<4>() << endl; // compiler error
return 0;
}
Is it possible to iterate over all Foo<i>
classes from which Bar
inherits? We can assume that i
is between 0 and some maximum N
. In pseudocode:
for ( int i = 0; i < N; i++ )
{
if ( Bar inherits from `Foo<i>` )
{
cout << Foo<i>::v << endl;
}
}
Here you go (live example):
Now
Bar
implementation holds all indices of base classes in variadic parameterI...
and derives all base classesFoo<I>...
in a variadic fashion.Function
loop
iterates over all bases using the auxiliarystruct
_do
. You can pass an arbitrary function, lambda or function objectf
toloop
. Thenf
will be called ongetValue<I>()
for allI...
, i.e., on memberv
of every base class.You use this as follows:
In other words, you don't need to detect if an object is an instance of some class. You make the object know its own bases by construction.
Now you may realize that if you make
Foo
a template on the member type (instead ofint
), you are quite close to start building your own very basic tuple.