vlad_tepesch vlad_tepesch - 3 months ago 11
C++ Question

call base class constructor without naming its class

class MyDerived: public Incredble<Difficult< And<Complicated, Long>>, And<Even, Longer>, BaseClass, Name>
{
public:
MyDerived();
}


MyDerived::MyDerived
: ???(params)
{}


Is there any way to call a base constructor without writing its full name and without typedeffing it?

The reason is clearly to avoid code duplication and introducing multiple positions to change if a detail in the base class template params changes.

Level 2 of this:

template <uint32 C>
class MyDerived: public Incredble<Difficult< And<Complicated, Long>>, And<Even, Longer>, BaseClass, Name>
{
public:
MyDerived();
}

template <uint32 C>
MyDerived::MyDerived<C>
: ???(C)
{
}

Answer

You could use injected-class-name. Incredible<...>::Incredible refers to itself, and since MyDerived isn't a class template, unqualified lookup will look in the scope of its base classes:

MyDerived::MyDerived
: Incredble(params)
{}

If that's not an option (i.e. MyDerived is actually a class template in your real code), then a type alias is the way to go. Probably in its own namespace:

namespace real {
    namespace detail {
        using derived_base = Incredible<...>;
    }

    class MyDerived : public detail::derived_base {
    public:
        MyDerived() : detail::derived_base(params) { }
    };
}

This would work with CRTP as well, just make derived_base an alias template.