Why does the forwarding link not display the rvalue link in the case of rvalue?

I understand that given the expression that initializes the reference / universal link, lvalues ​​are inferred of type T & and r values ​​of type T (not T &).

Thus, to allow only rvalues, you need to write

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

and not

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

My question is, why are rvalues ​​of type T displayed instead of T&& referral links? I think if they are derived as T && then the same link folding rule also works as T&& && is the same as T&& .

+4
source share
1 answer

Because at that time, the derivation of the arguments of rvalue A as A&& instead of A considered as an unnecessary complication and deviation from the usual 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

We did not even know if we could get an exception to the inference rules (for the case of lvalue A ), and it didn’t even occur to us what we would dare to ask with two exceptions. It would be useful for this: it makes it impossible, perhaps.

After all, without a single rule of special inference for the lvalue case, perfect forwarding is not possible, as N1385 showed.

Even with today's hindsight, adding another special deduction rule so that the client can avoid having to restrain the template restriction does not seem to be a very high cost-benefit ratio. Especially in comparison with the ratio of benefits / costs, which we filmed in 2002.

+2
source

Source: https://habr.com/ru/post/1479124/


All Articles