dvicino
dvicino

Reputation: 1509

Is possible to make a tuple of types with optional template parameters in C++14?

I'm working in a simulator. This simulator receives as input 2 kind of models, behavioural, and structural.

A behavioural model, is some any class that provides a few required functions and requires a template parameter "TIME".

A structural model, is a set of models (behavioural or structural) and a list of interactions between them. A structural model, does not provide behaviour, and as such does not require the parameter for TIME.

Example of behavioural model:

template<typename TIME>
struct B {...};

Example of Structural model:

template<typename connections, typename Ms>
struct S {
    template<typename TIME>
    using models=typename Ms::template type<TIME>;
    ...
};

The main problem is the "models" here. As it is listed there, every model gets a TIME template parameter, and I can get as many B models as I want. However, I cannot pass a S model to it.

My implementation of a models_tuple is the following

template<template<typename TIME> class... Ms>
struct models_tuple {
    template<typename T>
    using type=std::tuple<Ms<T>...>;
};

Is there any way that I can make the tuple receive both, classes with a Template parameter (Time) and others without it? I'm using static_assert for validating that classes are covering the requirements of implementing the functions.

Upvotes: 1

Views: 316

Answers (1)

T.C.
T.C.

Reputation: 137425

You can't have a mix of template template parameters and template type parameters. But you can wrap the former into a type.

template<template<class> class B>
struct wrap {};

Then, add the time, but only if it's the wrapper for the first kind of types:

template<class W, class>
struct maybe_apply_time { using type = W; };

template<class T, template<class> class B> 
struct maybe_apply_time<wrap<B>, T> { using type = B<T>; };

template<class... Ms>
struct models_tuple {
    template<typename T>
    using type=std::tuple<typename maybe_apply_time<Ms, T>::type...>;
};

And uses it as models_tuple<wrap<B>, S</*...*/> /*, etc. */>.

Upvotes: 4

Related Questions