Making a variadic templete from another variadic template

Sergio Pv :

To be honest I don't know how to start to search the solution for the problem I try to solve. Probably a solution already is. So the task is here.

I have a class that actually is template with 2 arguments:

template <typename F, typename S>
class trans {...};

also I have another class that holds a chain of these "trans" classes like a tuple (example):

class holder {
    using chain_type = std::tuple<trans<std::string, int>, 
                                  trans<int, float>, 
                                  trans<float, std::string>, 
                                  trans<std::string, json>>;
};

And as can be seen every second argument of a "trans" is the same as the next first. The chain:

std::string -> int -> float -> std::string -> json.

what would I want...I'd like to have some method to make this chain like this:

template <typename ...Args>
class holder {
    using chain_type = trans_chain_create_t<Args...>;
};

holder<std::string, int, float, std::string, json> h;

Is it possible? I'm not very familiar with variadic templates and use them very rare.

Andrey Semashev :

Yes, it is possible:

template< typename F, typename S >
class trans {};

template< typename F, typename S, typename... Tail >
struct create_trans_chain;

template< typename F, typename S, typename... Tail >
using create_trans_chain_t = typename create_trans_chain< F, S, Tail... >::type;

template< typename F, typename S >
struct create_trans_chain< F, S >
{
    using type = std::tuple< trans< F, S > >;
};

template< typename F, typename S, typename Next, typename... Tail >
struct create_trans_chain< F, S, Next, Tail... >
{
    using type = decltype(std::tuple_cat(
        std::declval< create_trans_chain_t< F, S > >(),
        std::declval< create_trans_chain_t< S, Next, Tail... > >()));
};

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=368968&siteId=1