C ++ 0x:迭代一个带有函数的元组 [英] C++0x: iterating through a tuple with a function

查看:88
本文介绍了C ++ 0x:迭代一个带有函数的元组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为 _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> 。这是我预期的工作方式:



查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆