加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 为什么std :: make_tuple将std :: reference_wrapper参数转

发布时间:2020-12-16 03:43:49 所属栏目:百科 来源:网络整理
导读:在C11标准中,它指出(参见 cppreference.com,另见标准的第20.4.2.4节),它指出 template class... Types tupleVTypes... make_tuple( Types... args ); Creates a tuple object,deducing the target type from the types of arguments. For each Ti in Types..
在C11标准中,它指出(参见 cppreference.com,另见标准的第20.4.2.4节),它指出
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 in Types...,the corresponding type Vi in Vtypes... is std::decay<Ti>::type unless application of std::decay results in std::reference_wrapper<X> for some type X,in which case the deduced type is X&.

我想知道为什么参考包装器在这里被处理得特别?

解决方法

这或多或少是reference_wrapper的主要目的.

通常,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来显式地请求它.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读