Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do const references extend the lifetime of rvalues?

Tags:

Why did the C++ committee decide that const references should extend the lifetime of temporaries?

This fact has already been discussed extensively online, including here on stackoverflow. The definitive resource explaining that this is the case is probably this GoTW:

GotW #88: A Candidate For the “Most Important const”

What was the rationale for this language feature? Is it known?

(The alternative would be that the lifetime of temporaries is not extended by any references.)


My own pet theory for the rationale is that this behavior allows objects to hide implementation details. With this rule, a member function can switch between returning a value or a const reference to an already internally existent value without any change to client code. For example, a matrix class might be able to return row vectors and column vectors. To minimize copies, either one or the other could be returned as a reference depending on the implementation (row major vs column major). Whichever one cannot be returned by reference must be returned by making a copy and returning that value (if the returned vectors are contiguous). The library writer might want leeway to change the implementation in the future (row major vs column major) and prevent clients from writing code that strongly depends on if the implementation is row major or column major. By asking clients to accept return values as const ref, the matrix class can return either const refs or values without any change to client code. Regardless, if the original rationale is known, I would like to know it.

like image 994
Praxeolitic Avatar asked Sep 27 '16 07:09

Praxeolitic


People also ask

What is the point of a const reference?

- const references allow you to specify that the data referred to won't be changed. A const reference is actually a reference to const. A reference is inherently const, so when we say const reference, it is not a reference that can not be changed, rather it's a reference to const.

What is one benefit of declaring the parameter as const reference?

Answer: The const qualifier Forbids the code to modify the argument, so the programmer can rest assured that the source object will remain unchanged.

Can a const reference change?

But const (int&) is a reference int& that is const , meaning that the reference itself cannot be modified.

Does const reference make a copy?

Not just a copy; it is also a const copy. So you cannot modify it, invoke any non-const members from it, or pass it as a non-const parameter to any function. If you want a modifiable copy, lose the const decl on protos .


1 Answers

It was proposed in 1993. Its purpose was to eliminate the inconsistent handling of temporaries when bound to references.

Back then, there was no such thing as RVO, so simply banning the binding of a temporary to a reference would have been a performance hit.

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/1993/N0345.pdf

like image 96
Richard Hodges Avatar answered Oct 20 '22 08:10

Richard Hodges