I have a template class for which the base class is also a template parameterized with a type member of one of the outer template type parameters. Example:
template <typename X>
class Adapter : public Generator<typename X::generated_type>{
using G = typename X::generated_type;
};
Here X
is some type that has a member generated_type
and we want to subclass a Generator
for X::generated_type
. Because X::generated_type
is a dependent name, we have to prefix it with typename
when we use it in the base class name.
In the rest of the definition of Adapter
, I can use G
instead of typename X::generated_type
, which is obviously a lot more convenient.
Is there any way to use G
in the name of the base class too?
In the actual code it's not this simple; I've tried to distill the example down to the core issue.
The only thing I could think of was to have G
as a template parameter and create a wrapper that passes X::generated_type
as G
.
One possible solution is to cheat, something along these lines:
template <typename X, typename G=typename X::generated_type>
class Adapter : public Generator<G>{
// ...
};
You were almost there, heading into an additional template parameter territory, but you don't need to explicitly pass it in, just default it.
Complete example:
template<typename> class Generator {};
template <typename X, typename G=typename X::generated_type>
class Adapter : public Generator<G>{
// ...
};
struct Y {
typedef int generated_type;
};
int main()
{
Adapter<Y> a;
return 0;
}