template< class... Types > tuple<VTypes...> make_tuple( Types&&... args );
Creates a tuple object,deducing the target type from the types of arguments.
For each
Ti@H_403_9@ in
Types...@H_403_9@,the corresponding type
Vi@H_403_9@ in
Vtypes...@H_403_9@ is
std::decay<Ti>::type@H_403_9@ unless application of
std::decay@H_403_9@ results in
std::reference_wrapper<X>@H_403_9@ for some type
X@H_403_9@,in which case the deduced type is
X&@H_403_9@.
我想知道为什么参考包装器在这里被处理得特别?
解决方法
通常,std :: make_tuple总是使值的元组(std :: decay模拟逐个值语义).给定int x,y; std :: make_tuple(x,y);使一个std :: tuple< int,int>,即使它将推断类型为一组参考int& int& std :: decay将那些转换为int,int.
reference_wrapper允许您强制创建引用的元组:std :: make_tuple(std :: ref(x),y)将使一个std :: tuple< int& int>.
标准库的其他部分以相同的方式使用reference_wrapper.例如,std :: bind通常会将绑定的参数复制/移动到生成的对象中,但如果您希望它仅存储引用,则可以通过传递一个reference_wrapper来显式地请求它.