Why do const references extend the lifetime of rvalues? : <p>Why did the C++ committee decide that const references should extend the lifetime of temporaries?</p>

<p>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:</p>

<p><a href="https://herbsutter.com/2008/01/01/gotw-88-a-candidate-for-the-most-important-const/" rel="noreferrer">GotW #88: A Candidate For the “Most Important const”</a></p>

<p>What was the rationale for this language feature? Is it known?</p>

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

<hr>

<p>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.</p>
