Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

(rvalue reference) VS (const lvalue reference) as function parameters in C++11

Can someone explain when rvalue references are preferred over const lvalue references when working as function parameters?

Background: I was trying to pass a const pointer into a function. Since I have to consider the cases in which a local pointer is passed in and in which a temporary is passed in (say from a function call return), I have two choices: the parameter could either be declared as:

void foo(T const* const&); //const lvalue ref to const ptr

or

void foo(T const* &&); //rvalue ref to const ptr

But this rvalue reference cannot be bound to a local variable (which is of lvalue type. But I did remember Scott Meyers coined the term "universal reference" to refer to rvalue reference. This confuses me more.) So my question is, since the first declaration could deal with both cases, when would the second one using rvalue reference be preferred?

Note: In the first approach, the other forms

void foo(const const T* &); 
void foo(const T* const&); 

didn't work. I guess the reason is that in the latter two I was not consistent in the place where the const qualifiers are put into (please correct me if I'm wrong).

like image 211
CloudyTrees Avatar asked Jan 03 '14 05:01

CloudyTrees


People also ask

What is the difference between lvalue and rvalue references?

“l-value” refers to a memory location that identifies an object. “r-value” refers to the data value that is stored at some address in memory. References in C++ are nothing but the alternative to the already existing variable.

What is R Value reference in C ++ 11?

An rvalue reference is a reference that will bind only to a temporary object. What do I mean? Prior to C++11, if you had a temporary object, you could use a "regular" or "lvalue reference" to bind it, but only if it was const: 1. 2.

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.

Can lvalue bind to rvalue reference?

An lvalue reference can bind to an lvalue, but not to an rvalue.


1 Answers

It is very rarely a good idea to pass a pointer by const &: at best it takes the same overhead, at worst it causes extremely complex pointer reseating logic to surprise readers of your code.

Take pointers by value -- T const* -- and things are more sane.

References to non-pointer values make more sense.

Universal references is a technique using rvalue and lvalue references in a type deduction context. It basically only applies when you have a type T&& being deduced from an expression -- in that context T can be X, X& or X const& (or other cv variants).

If T is X& or X const&, the rvalue reference to the lvalue reference collapses into a lvalue reference. It is an example of the standard committee being clever, and it allows auto&&x= based universal reference variables, and perfect forwarding code to be easy to write.

like image 161
Yakk - Adam Nevraumont Avatar answered Sep 19 '22 01:09

Yakk - Adam Nevraumont