2012-01-05 85 views
0

我有点困惑,如果有可能如何使用可变元组作为函数的参数以及如何初始化它。元组作为函数参数

template <typename T, Arg ...> 
     void foo (int a, std::tuple<T, sizeof(Arg)> TupleTest); 
... 

foo(TupleTest(2, "TEST", 5.5)); 

这怎么可以用C++ 0x实现?

+1

这有点不清楚 - 你想达到什么目的?你可以说'template void foo(std :: tuple t){/ * ... * /}'。 – 2012-01-05 18:19:02

+0

什么是TupleTest?它是一种元组吗?为什么'int a'在Tuple之外?把sizeof(Arg)'放在那里有什么意义? – kennytm 2012-01-05 18:27:13

回答

6

您不需要获取模板参数的数量。只要这样做:

template <typename... T> 
void foo(int a, std::tuple<T...> TupleTest); 

// make_tuple so we don't need to enter all the type names 
foo(0, std::make_tuple(2, "TEST", 5.5)); 
0

你想sizeof?只需使用一个可变扩展:

template <typename T, typename Arg ...> 
void foo(int a, std::tuple<T, Arg...> TupleTest); 

这里,TupleTest是参数,不是一个类型名称的名称。所以在调用方法时,不要使用它。

foo(42, std::tuple<int, char const*, double>(2, "TEST", 5.5)); 

最后,类型参数T没有实际意义(除非您想禁止一个空的模板列表),这样你可以删除它没有损失。