C ++ 11:我可以从多个args到tuple,但是我可以从tuple到多个args? [英] C++11: I can go from multiple args to tuple, but can I go from tuple to multiple args?

查看:513
本文介绍了C ++ 11:我可以从多个args到tuple,但是我可以从tuple到多个args?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

如何将元组扩展为可变模板函数的参数?

解包一个元组调用匹配函数指针


在C ++ 11模板中,有没有办法使用元组作为(可能是模板)函数的个别参数?



例如:

假设我有这个函数:

  void foo(int a,int b)
{
}

我有元组 auto bar = std :: make_tuple(1,2)



我可以用模板方式调用 foo(1,2) / p>

我不是简单地 foo(std :: get< 0>(bar),std :: get< 1>

更完整的例子:

strong>

 模板< typename Func,typename ... Args> 
void caller(Func func,Args ... args)
{
auto argtuple = std :: make_tuple(args ...);
do_stuff_with_tuple(argtuple);
func(insert_magic_here(argtuple)); //< - 这是很难的部分
}

'

解决方案

尝试类似的工具这个:

  //实现细节,用户从来不直接调用
命名空间细节
{
template< typename F,typename Tuple,bool Done,int Total,int ... N>
struct call_impl
{
static void call(F f,Tuple& t)
{
call_impl< F,Tuple,Total == 1 + sizeof ...(N),Total,N ...,sizeof ...(N)> :: call(f,std :: forward
}
};

template< typename F,typename Tuple,int Total,int ... N>
struct call_impl< F,Tuple,true,Total,N ...>
{
static void call(F f,Tuple& t)
{
f(std :: get< N>(std :: forward< Tuple> ))...);
}
};
}

//用户调用这个
模板< typename F,typename Tuple>
void call(F f,Tuple& t)
{
typedef typename std :: decay< Tuple&
detail :: call_impl< F,Tuple,0 == std :: tuple_size< ttype> :: value,std :: tuple_size< ttype> :: value> :: call(f,std :: forward< Tuple> ;(t));
}

示例:

  #include< cstdio> 
int main()
{
auto t = std :: make_tuple(%d,%d,%d \\\
,1,2,3)
call(std :: printf,t);
}

使用一些额外的魔法并使用 std :: result_of ,你也可以让整个函数返回正确的返回值。


Possible Duplicate:
How do I expand a tuple into variadic template function's arguments?
“unpacking” a tuple to call a matching function pointer

In C++11 templates, is there a way to use a tuple as the individual args of a (possibly template) function?

Example:
Let's say I have this function:

void foo(int a, int b)  
{  
}

And I have the tuple auto bar = std::make_tuple(1, 2).

Can I use that to call foo(1, 2) in a templaty way?

I don't mean simply foo(std::get<0>(bar), std::get<1>(bar)) since I want to do this in a template that doesn't know the number of args.

More complete example:

template<typename Func, typename... Args>  
void caller(Func func, Args... args)  
{  
    auto argtuple = std::make_tuple(args...);  
    do_stuff_with_tuple(argtuple);  
    func(insert_magic_here(argtuple));  // <-- this is the hard part  
}

I should note that I'd prefer to not create one template that works for one arg, another that works for two, etc…

解决方案

Try something like this:

// implementation details, users never invoke these directly
namespace detail
{
    template <typename F, typename Tuple, bool Done, int Total, int... N>
    struct call_impl
    {
        static void call(F f, Tuple && t)
        {
            call_impl<F, Tuple, Total == 1 + sizeof...(N), Total, N..., sizeof...(N)>::call(f, std::forward<Tuple>(t));
        }
    };

    template <typename F, typename Tuple, int Total, int... N>
    struct call_impl<F, Tuple, true, Total, N...>
    {
        static void call(F f, Tuple && t)
        {
            f(std::get<N>(std::forward<Tuple>(t))...);
        }
    };
}

// user invokes this
template <typename F, typename Tuple>
void call(F f, Tuple && t)
{
    typedef typename std::decay<Tuple>::type ttype;
    detail::call_impl<F, Tuple, 0 == std::tuple_size<ttype>::value, std::tuple_size<ttype>::value>::call(f, std::forward<Tuple>(t));
}

Example:

#include <cstdio>
int main()
{
    auto t = std::make_tuple("%d, %d, %d\n", 1,2,3);
    call(std::printf, t);
}

With some extra magic and using std::result_of, you can probably also make the entire thing return the correct return value.

这篇关于C ++ 11:我可以从多个args到tuple,但是我可以从tuple到多个args?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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