推入和弹出std::元组的第一个元素

Pushing and popping the first element of a std::tuple

本文关键字:元组 第一个 元素 std      更新时间:2023-10-16

我正在用c++写一个函数,用这种方式有可变数量的参数(和不同类型)

template<typename ...Ts>
void myFunction(Ts ...args)
{
    //create std::tuple to access and manipulate single elements of the pack
    auto myTuple = std::make_tuple(args...);    
    //do stuff
    return;
}

我想做什么,但我不知道怎么做,是从元组中推送和弹出元素,特别是第一个元素…就像

//remove the first element of the tuple thereby decreasing its size by one
myTuple.pop_front()
//add addThis as the first element of the tuple thereby increasing its size by one
myTuple.push_front(addThis)

这可能吗?

你可以这样做

template <typename T, typename Tuple>
auto push_front(const T& t, const Tuple& tuple)
{
    return std::tuple_cat(std::make_tuple(t), tuple);
}
template <typename Tuple, std::size_t ... Is>
auto pop_front_impl(const Tuple& tuple, std::index_sequence<Is...>)
{
    return std::make_tuple(std::get<1 + Is>(tuple)...);
}
template <typename Tuple>
auto pop_front(const Tuple& tuple)
{
    return pop_front_impl(tuple,
                          std::make_index_sequence<std::tuple_size<Tuple>::value - 1>());
}

演示

请注意,它是非常基本的,不处理引用元组或const限定类型的元组,但它可能足够了。

使用泛型lambda,您可以做得非常优雅:

template<typename Tuple>
constexpr auto pop_front(Tuple tuple) {
    static_assert(std::tuple_size<Tuple>::value > 0, "Cannot pop from an empty tuple");
    return std::apply(
        [](auto, auto... rest) { return std::make_tuple(rest...); }, 
        tuple);
}

std::tuple的长度和类型在编译时确定。不可能出现运行时弹出或推送。您可以使用std::vector来提供运行时修改。

您的向量的数据类型可以是std::variant (c++ 17)或boost::variant。这两种类型在编译时都接受一个支持类型的列表,并且可以用匹配类型的任何值填充。

或者,您可以使用std::any(也是c++ 17)或boost::any来存储任何类型,但具有不同的访问语义。

typedef boost::variant<int, std::string, double> value;
std::vector<value> data;
data.push_back(value(42));
data.psuh_back(value(3.14));

您不能通过添加元素来'延长'元组-这不是元组所代表的。元组的意义在于组成它的不同值之间的绑定连接,如'firstname', 'lastname', 'phone'。

你想要的似乎更容易完成使用一个向量-你可以很容易地添加元素,并删除它们-它的大小可以根据需要任意更改。