Emm*_*et 3 c++ variadic-functions variadic-templates c++11
我正在用这种方式在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;
}
Run Code Online (Sandbox Code Playgroud)
我想做什么,但我不知道怎样,是从元组推送和弹出元素,特别是第一个元素...类似的东西
//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)
Run Code Online (Sandbox Code Playgroud)
这可能吗?
你可以做点什么
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>());
}
Run Code Online (Sandbox Code Playgroud)
请注意,它实际上是基本的,不处理引用元组或const限定类型的元组,但它可能就足够了.