Ask Your Question
0

How can I access individual aliases that are included in a parameter pack using a fold expression within another alias?

asked 2021-09-09 11:00:00 +0000

lakamha gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2021-08-16 20:00:00 +0000

woof gravatar image

You can access individual aliases that are included in a parameter pack using a fold expression within another alias by implementing the following steps:

  1. Define the initial alias that uses the parameter pack and a fold expression to generate a sequence of types.
template<typename... Ts>
using TypeList = std::tuple<Ts...>;

template<typename... Ts>
using TypeListFromTuple = TypeList<std::decay_t<decltype(std::get<Ts>(std::declval<std::tuple<Ts...>>()))>...>;

Here, the TypeListFromTuple alias takes a tuple of types as its input and generates a TypeList that contains the same types.

  1. Define the sequence of indices that correspond to the types in the TypeList.
template<std::size_t... Is>
using IndexList = std::index_sequence<Is...>;

template<std::size_t N>
using MakeIndexList = std::make_index_sequence<N>;

Here, the IndexList alias represents a sequence of indices, and the MakeIndexList alias generates a sequence of indices from 0 to N-1.

  1. Define the final alias as a function that takes the parameter pack as its input and generates a tuple of types.
template<typename... Ts>
constexpr auto MakeTypeTuple(Ts...)
{
    return std::make_tuple(typeid(Ts)...);
}

template<typename... Ts>
using TypeTuple = decltype(MakeTypeTuple(std::declval<Ts>()...));

Here, the MakeTypeTuple function takes the parameter pack as its input and generates a tuple of type_info objects that correspond to the types in the pack. The TypeTuple alias uses decltype to extract the type of the tuple that is returned by MakeTypeTuple.

  1. Combine the previous aliases into a single alias that takes the parameter pack as its input and generates a tuple of types.
template<typename... Ts>
using FunctionTypes = TypeListFromTuple<TypeTuple<Ts...>>;

template<typename... Ts>
using FunctionTypesIndexSeq = IndexList<std::tuple_size_v<TypeTuple<Ts...>>>;

template<typename... Ts>
using FunctionIndices = MakeIndexList<sizeof...(Ts)>;

template<typename... Ts>
using FunctionTypesWithIndices = std::pair<FunctionTypes<Ts...>, FunctionTypesIndexSeq<Ts...>>;

template<typename... Ts>
constexpr auto GetFunctionTypes(Ts...)
{
    return FunctionTypesWithIndices<Ts...>{};
}

template<typename... Ts>
using FunctionTypeTuple = std::tuple<typename std::tuple_element_t<FunctionTypesIndexSeq<Ts...>::value, FunctionTypes<Ts...>>...>;

template<typename... Ts>
using FunctionTypeList = TypeList<typename std::tuple_element_t<FunctionTypesIndexSeq<Ts...>::value, FunctionTypes<Ts...>>...>;

Here, the FunctionTypes alias generates a TypeList of the types in the parameter pack. The FunctionTypesIndexSeq alias generates an IndexList that corresponds to the indices of the types in the TypeList. The FunctionIndices alias generates a sequence of indices from 0 to N-1, where N is the number of types in the parameter pack. The FunctionTypesWithIndices alias combines the FunctionTypes and FunctionTypesIndexSeq aliases into a single pair. The GetFunctionTypes function takes the parameter pack as its input and generates a pair of FunctionTypes and FunctionTypesIndexSeq. The FunctionTypeTuple alias uses the std::tuple_element_t trait to extract the type at the index specified by FunctionTypesIndexSeq. The FunctionTypeList alias generates a TypeList of the types in the parameter pack, using std::tuple_element_t to extract the types from the FunctionTypes alias.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2021-09-09 11:00:00 +0000

Seen: 10 times

Last updated: Aug 16 '21