Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why forwarding reference does not deduce to rvalue reference in case of rvalue?

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) {}

and not,

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

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&&.

like image 282
abir Avatar asked May 04 '13 11:05

abir


People also ask

What is forwarding reference?

I understand that a forwarding reference is "an rvalue reference to a cv-unqualified template parameter", such as in. template <class T> void foo(T&& ); which means the above function can take both l-value and r-value reference.

Can you pass an lvalue to an rvalue reference?

By overloading a function to take a const lvalue reference or an rvalue reference, you can write code that distinguishes between non-modifiable objects (lvalues) and modifiable temporary values (rvalues). You can pass an object to a function that takes an rvalue reference unless the object is marked as const .

How do you pass rvalue reference to a function?

If you want pass parameter as rvalue reference,use std::move() or just pass rvalue to your function.

Is an rvalue reference an rvalue?

An rvalue reference is formed by placing an && after some type. An rvalue reference behaves just like an lvalue reference except that it can bind to a temporary (an rvalue), whereas you can not bind a (non const) lvalue reference to an rvalue.


1 Answers

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

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.

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.

like image 62
Howard Hinnant Avatar answered Sep 27 '22 17:09

Howard Hinnant