我正在用这种方式在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限定类型的元组,但它可能就足够了。
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));
使用通用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);
}
你不能通过添加元素来“延长”元组 - 这不是元组所代表的。元组的要点是构成它的不同值之间的绑定连接,如'firstname','lastname','phone'。
您似乎想要的更容易通过使用向量来实现 - 您可以轻松地向其添加元素并删除它们 - 其大小可以根据需要任意更改。