为元组实现创建索引序列

Making an index sequence for tuple implementation

本文关键字:索引 创建 元组 实现      更新时间:2023-10-16

假设我想实现类似std::tuple的东西,只是基础知识。我想先展示一次失败的尝试。

#include <utility>
#include <iostream>
template <std::size_t I>
struct tuple_index_leaf {
using Index = std::integral_constant<std::size_t, I>;
std::size_t i = Index::value;
};
template <std::size_t... Is>
struct tuple_index : tuple_index_leaf<Is>...
{};
template <std::size_t I, std::size_t... Is>
constexpr auto get_index(tuple_index<Is...> const &i) {
return static_cast<const tuple_index_leaf<I>*>(&i)->i;
}
template <std::size_t I, typename T>
struct tuple_leaf : tuple_index_leaf<I> {
T elem;
};
template<typename... Ts>
struct tuple : tuple_leaf<sizeof...(Ts), Ts>... {
};
template <std::size_t I, typename... Ts>
auto& get(tuple<Ts...> &t) {
return static_cast<tuple_leaf<I, float>*>(&t)->elem;
}
int main() {
tuple_index<0, 1, 2> ti;
std::cout << get_index<0>(ti) << "n";
tuple<int, float> t;
get<2>(t) = 3.14;
} 

现在,看看get函数。我对最后一个类型float进行了硬编码,我只能用索引 2 调用它,就像get<2>一样。这是因为我的tuple构造函数存在缺陷。如果你看那里,你会发现我正在sizeof...(Ts)传递给tuple_leaf.例如,在这种情况下,我所有的元组叶子都像tuple_leaf<2, int>, tuple_leaf<2, float>。我想要的是像tuple_leaf<0, int>, tuple_leaf<1, float>...这样的扩展。我知道,我使用的扩展tuple_leaf<sizeof...(Ts), Ts>...没有给我这些。我需要某种索引序列,我想并开始实现类似tuple_index.但是那个需要我通过std::size_t...,我不知道该怎么做。所以问题是,我怎样才能获得像tuple_leaf<0, int>, tuple_leaf<1, float>...这样的扩展?

这并不难。这里有一个例子是如何做到这一点的(不是声称唯一的方法,这是我快速整理的东西):

#include <utility>
#include <cstddef>
template <std::size_t I, typename T>
struct tuple_leaf {
T elem;
};
template<class SEQ, class... TYPE> struct tuple_impl;
template<size_t... Ix, class... TYPE>
struct tuple_impl<std::index_sequence<Ix...>, TYPE...> : tuple_leaf<Ix, TYPE>... { };
template<typename... Ts>
struct tuple : tuple_impl<std::make_index_sequence<sizeof...(Ts)>, Ts...> { };

// below lines are for testing
tuple<int, double, char*> tup;
// the fact that this compiles tells us char* has index 2
auto& z = static_cast<tuple_leaf<2, char*>&>(tup);