You can access individual aliases that are included in a parameter pack using a fold expression within another alias by implementing the following steps:
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.
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.
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
.
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.
Asked: 2021-09-09 11:00:00 +0000
Seen: 10 times
Last updated: Aug 16 '21