如何通过索引获取列表类型的元素

Chr*_* G. 4 c++ metaprogramming variadic-templates c++20

如何通过索引(例如std::tuple_element )(最好以非递归方式)using L = type_list<T1, T2, ...>检索类型列表的元素?

我想避免使用元组作为用例的类型列表,这需要实例化来传递像f(L{}).

template<typename...> struct type_list {};
using L = typelist<int, char, float, double>;
using T = typeAt<2, L>; // possible use case
Run Code Online (Sandbox Code Playgroud)

不确定使用std::index_sequence进行迭代并通过索引版本 的std::is_same进行测试是否是一个好的方法。std::integral_constant

pao*_*olo 6

我想避免使用元组作为用例的类型列表,这需要实例化来传递列表,例如f(L{})

如果您不想实例化std::tuple,但在未评估的环境中可以接受它,您可以利用来std::tuple_element实现您的typeAt特质:

template <std::size_t I, typename T>
struct typeAt;

template <std::size_t I, typename... Args>
struct typeAt<I, type_list<Args...>> : std::tuple_element<I, std::tuple<Args...>> {};
                                    // ^ let library authors do the work for you

using L = type_list<int, char, float, double>;
using T = typename typeAt<2, L>::type;

static_assert(std::is_same<T, float>::value, "");
Run Code Online (Sandbox Code Playgroud)