2

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 ?

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

C++17 will buy you heterogeneous variadic non-type parameters, but still no mix.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.