The next code works fine (this is a oversimplified version of other problem of mine, with types longer, deeper and more templates):
template<class C>
struct Base
{};
template<class C>
struct Derived : public Base<C>
{
Derived() : Base<C>()
{}
};
But, how could I call to the base class constructor without “write” the complete type of its base class? For example, I tried something like:
template<class C>
struct Base
{
typedef Base base_type;
};
template<class C>
struct Derived : public Base<C>
{
Derived() : base_type() {}
};
int main()
{
Derived<void> b;
}
But “base_type” isn’t recognized. The message that gcc throws is:
test3.cpp: In constructor 'Derived<C>::Derived()':
test3.cpp:100:17: error: class 'Derived<C>' does not have any field
named 'base_type'
To solve it I have to write Base<C>::base_type in the constructor but this would make the existence of base_type itself irrelevant.
Is it my campaign of writting-saving impossible?
And, why base_type in the constructor isn’t found, and however this works fine?
int main()
{
Derived<void>::base_type b;
}
EDIT: With the comment of @Jack Aidley the best form I’ve found to get the type of the base class with a simple alias is:
template<typename C> struct Base {};
template<typename C, typename Base>
struct Derived_impl : public Base
{
Derived_impl() : Base()
{}
};
template<typename C>
using Derived = Derived_impl<C, Base<C> >;
int main()
{
Derived<void> b;
}
According to the standard
It means, that you have to tell to the compiler, that
base_typein theBaseclass, that depends ofC. You can use, for example, this:or this