C ++ 0x:迭代一个带有函数的元组 [英] C++0x: iterating through a tuple with a function
问题描述
我有一个名为 _push
的函数,它可以处理不同的参数,包括元组,并且应该返回推送元素的数量。
例如, _push(5)
应该在堆栈上按下'5'( _push(std :: make_tuple() (5,hello))应该按'5'和'hello'并返回2。
_push(5,hello)
因为我有时使用 _push(foo())
foo()
返回一个元组。
无论如何我不能使它与元组一起工作: / p>
template< typename ... Args,int N = sizeof ...(Args)>
int _push(const std :: tuple< Args ...>& t,typename std :: enable_if<(N> = 1)> :: type * = nullptr){
return _push< Args ...,N-1>(t)+ _push(std :: get< N-1(t));
}
template< typename ... Args,int N = sizeof ...(Args)>
int _push(const std :: tuple< Args ...>& t,typename std :: enable_if<(N == 0)> :: type * = nullptr){
return 0;
}
假设您要推送 tuple< int ,bool>
。这是我预期的工作方式:
-
_push< {int,bool},2> $ (第一定义)调用(第一定义)
-
_push< {int,bool},1>
-
_push< {int,bool},0>
ul>
然而,使用g ++ 4.5(我支持可变模板的唯一编译器),我得到一个关于
_push< Args ...,N-1> ;(t)
(第3行)说它找不到匹配的函数调用(没有任何进一步的细节)。我尝试没有...但我得到另一个错误,说明参数包不扩展。
如何解决这个问题?
PS:我知道你可以使用模板struct(这实际上是我之前做的),但我想知道如何做一个
>解决方案
我解决了一些黑客的问题。这里是代码:
template< typename ... Args,int N = sizeof ...(Args)>
int _push(const std :: tuple< Args ...>& t,std :: integral_constant< int,N> * = nullptr,typename std :: enable_if& ; :: type * = nullptr){
return _push(t,static_cast< std :: integral_constant< int,N-1>(nullptr))+ _push(std :: get< N-1> (t));
}
template< typename ... Args,int N = sizeof ...(Args)>
int _push(const std :: tuple< Args ...>& t,std :: integral_constant< int,N> * = nullptr,typename std :: enable_if<(N == 0)> :: type * = nullptr){
return 0;
}
如果找到更好的办法,请不要犹豫发布
I have a function named
_push
which can handle different parameters, including tuples, and is supposed to return the number of pushed elements.For example,
_push(5)
should push '5' on the stack (the stack of lua) and return 1 (because one value was pushed), while_push(std::make_tuple(5, "hello"))
should push '5' and 'hello' and return 2.I can't simply replace it by
_push(5, "hello")
because I sometimes use_push(foo())
and I want to allowfoo()
to return a tuple.Anyway I can't manage to make it work with tuples:
template<typename... Args, int N = sizeof...(Args)> int _push(const std::tuple<Args...>& t, typename std::enable_if<(N >= 1)>::type* = nullptr) { return _push<Args...,N-1>(t) + _push(std::get<N-1>(t)); } template<typename... Args, int N = sizeof...(Args)> int _push(const std::tuple<Args...>& t, typename std::enable_if<(N == 0)>::type* = nullptr) { return 0; }
Let's say you want to push a
tuple<int,bool>
. This is how I expect it to work:_push<{int,bool}, 2>
is called (first definition)_push<{int,bool}, 1>
is called (first definition)_push<{int,bool}, 0>
is called (second definition)
However with g++ 4.5 (the only compiler I have which supports variadic templates), I get an error concerning
_push<Args...,N-1>(t)
(line 3) saying that it couldn't find a matching function to call (without any further detail). I tried without the "..." but I get another error saying that the parameters pack is not expanded.How can I fix this?
PS: I know that you can do this using a template struct (this is in fact what I was doing before), but I'd like to know how to do it with a function
PS 2: PS2 is solved, thanks GMan
解决方案I solved the problem with some hacks. Here is the code:
template<typename... Args, int N = sizeof...(Args)> int _push(const std::tuple<Args...>& t, std::integral_constant<int,N>* = nullptr, typename std::enable_if<(N >= 1)>::type* = nullptr) { return _push(t, static_cast<std::integral_constant<int,N-1>*>(nullptr)) + _push(std::get<N-1>(t)); } template<typename... Args, int N = sizeof...(Args)> int _push(const std::tuple<Args...>& t, std::integral_constant<int,N>* = nullptr, typename std::enable_if<(N == 0)>::type* = nullptr) { return 0; }
Don't hesitate to post if you find a better way
这篇关于C ++ 0x:迭代一个带有函数的元组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!