如何对可变函数中的所有参数调用std :: forward? [英] How would one call std::forward on all arguments in a variadic function?

查看:137
本文介绍了如何对可变函数中的所有参数调用std :: forward?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是写一个通用对象工厂,并使用boost预处理器元库来制作一个可变参数模板(使用2010,它不支持它们)。我的函数使用rval引用和std :: forward做完美的转发,它让我想...当C ++ 0X出来,我有一个标准的编译器,我会做真正的可变模板。如何,我可以在参数上调用std :: forward?

I was just writing a generic object factory and using the boost preprocessor meta-library to make a variadic template (using 2010 and it doesn't support them). My function uses rval references and std::forward to do perfect forwarding and it got me thinking...when C++0X comes out and I had a standard compiler I would do this with real variadic templates. How though, would I call std::forward on the arguments?

template< typename ... Params>
void f(Params ... params)//我如何说这些是rvalue引用?
{
y(std :: forward(... params)); //? - 我怀疑这将工作。
}

template < typename ... Params > void f(Params ... params) // how do I say these are rvalue reference? { y(std::forward(...params)); //? - I doubt this would work. }

只有我想到的方式才需要手动解包。 ..params和我还不完全在那里。是否有更快的语法可以工作?

Only way I can think of would require manual unpacking of ...params and I'm not quite there yet either. Is there a quicker syntax that would work?

推荐答案

你会这样做:

template <typename ...Params>
void f(Params&&... params)
{
    y(std::forward<Params>(params)...);
}

... 几乎说拿左边的,并为每个模板参数,相应地解压缩。

The ... pretty much says "take what's on the left, and for each template parameter, unpack it accordingly."

这篇关于如何对可变函数中的所有参数调用std :: forward?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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