为什么在rvalue的情况下转发引用不演绎为rvalue引用? [英] Why forwarding reference does not deduce to rvalue reference in case of rvalue?

查看:61
本文介绍了为什么在rvalue的情况下转发引用不演绎为rvalue引用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我了解到,给定一个初始化转发/通用引用的表达式,就可以推断出左值是 T& 类型,而右值是 T类型(而不是 T& )。

I understand that, given an expression initializing a forwarding/universal reference,lvalues are deduced to be of type T& and rvalues of type T (and not T&&).

因此,仅允许右值,则需要编写

Thus,to allow only rvalues, one need to write

template<class T, enable_if<not_<is_lvalue_reference<T> >,OtherConds... > = yes>
void foo(T&& x) {}

而不是

template<class T, enable_if<is_rvalue_reference<T>,OtherConds... > = yes>
void foo(T&& x) {}

我的问题是,为什么要转发引用时,推导得出右值是 T 类型,而不是 T& 类型?我猜想,如果将它们推导为 T& ,那么相同的引用折叠规则也将作为 T&& && T&& 相同。

My question is , why for forwarding references, rvalues are deduced to be of type T and not T&& ? I guess, if they are deduced as T&& then also same referencing collapsing rule works as T&& && is same as T&&.

推荐答案

因为当时,将右值 A 参数推导出为 A& 而不是 A 被视为不必要的并发症,并偏离了正常的扣除规则:

Because at the time, deducing rvalue A arguments as A&& instead of A was seen as an unnecessary complication and departure from the normal rules of deduction:

http://www.open-std.org/jtc1/sc22/wg21/ docs / papers / 2002 / n1385.htm

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2002/n1377.htm

我们甚至都不知道是否可以得到扣除规则的一个例外(对于左值 A ) ,而且我们从来没有想到我们敢要求两个例外。这样做的好处是必须:它使不可能的事情成为可能。

We really didn't even know if we could get one exception of the deduction rules (for the lvalue A case), and it never even occurred to us that we would dare ask for two exceptions. To do so, the benefit would have had to have been: It makes what is impossible, possible.

毕竟,对于左值案例,没有单一的特殊扣除规则,

After all, without the single special deduction rule for the lvalue case, perfect forwarding is impossible, as N1385 so aptly demonstrated.

即使今天事后看来,添加另一条特殊的推导规则也可以使客户避免否定模板约束,这是不可能的。似乎没有很高的收益/成本比。特别是与我们在2002年所追求的收益/成本比相比。

Even with today's hindsight, adding another special deduction rule so that the client can avoid having to negate a template constraint, does not seem like a very high benefit/cost ratio. Especially compared to the benefit/cost ratio we were shooting for in 2002.

这篇关于为什么在rvalue的情况下转发引用不演绎为rvalue引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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