rafoo
rafoo

Reputation: 1642

Inheritance with same default template arguments

Given the following class:

template<typename BoringArg1 = int,
         typename BoringArg2 = char,
         typename ThisDefinitionIsSoLong = int,
         typename WhoWroteThis = double>
class A {
    // ...
};

Now I want to inherit from A, keeping it templated:

template<typename BoringArg1 = int,
         typename BoringArg2 = char,
         typename ThisDefinitionIsSoLong = int,
         typename WhoWroteThis = double>
class B : public A<BoringArg1, BoringArg2, ThisDefinitionIsSoLong, WhoWroteThis> {
    // ...
};

As you can see, the templated arguments are repeated. Is it possible to avoid this ? Moreover, the default arguments of A may change. It would be nice to reflect these change in B without code modification in B. One solution I see is to make a bunch of template type DefaultTypeXXX. It's a bit tedious, and can't really change the order and count of template arguments of A (but this condition is lesser important). Is there any alternative ?

Upvotes: 2

Views: 69

Answers (1)

If all your parameters are of the same sort (types, non-types or templates), you may let B accept a parameter pack

template<typename... Args>
class B : public A<Args...> {
// ...
};

This will either forward the corresponding arguments to A, or use the default if the pack isn't long enough.

Upvotes: 2

Related Questions