【发布时间】:2016-12-30 07:10:23
【问题描述】:
我正在用这种方式用 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)
这可能吗?
【问题讨论】:
-
可以,但不能作为成员函数。
-
它的大小是一个编译时属性(模板参数列表的长度)。您不能重复使用相同的类型。
-
@Jarod42 那怎么办?我应该使用哪些功能?
-
您要在第一个位置插入的新元素(
addThis)是相同的还是另一个元素?在第二种情况下,新元素的类型是不同的类型?
标签: c++ c++11 variadic-templates variadic-functions