If I use a chain of inheritance like the following example I could use vars from the deepest base without any problems:
class A { public: int x; };
class B : public A { };
class C: public B { public: void Do() { cout << x << endl; } };
If I do the same with recursive variadic template classes I could not access my vars. Any idea how to access the vars and why I could not see my vars?
template <class ...Parms>
class Example;
template <class Head, class ...Parms>
class Example<Head, Parms...>: public Example<Parms...>
{
};
template <>
class Example<>
{
public:
int x;
};
template <class ...Parms>
class Last: public Example<Parms...>
{
void Do() { cout << x << endl; }
};
Compile fails before any instance of the class is instantiated!
xis a dependent name in this case, so you must access if asthis->x(or bring it into scope by putting a using declaration in your class definition:EDIT
The reason for this is discussed in [temp.res] in the standard. Basically: when the compiler is parsing your template, it sees just
x. It has no way of knowing thatxdepends on the template’s parameters (which it does in your case, because it comes from a base class which depends on them). Therefore, the compiler tries to resolvexusing what it knows when parsing the template, and fails.Writing
this->xindicates thatxrefers to a member of the class; since a base class of your particular class depends on template parameters, the compiler knows it cannot resolvexwhen parsing the template, and will postpone the resolution until the template is instantiated. At that time, the template arguments are known.The same holds true for
using Example<Params...>::x;. That also tells the compiler thatxdepends on template parameters, and its resolution must be postponed to instanitation.