I have the following function:
T foo();
And, in another function, I’m playing with the returned T value, like:
const T& t = foo();
or
T&& t = foo();
I want to ask:
-
Are both equivalent?
-
Memory-wise, what are the difference between these two approaches?
-
I know that on the first approach the temporary value lifetime matches the reference lifetime. The same occurs when using the rvalue reference?
-
Should I even consider using a const rvalue reference (
const T&&)?
I’m using the first approach, that is what I’m used to and attends me fine. I’m still wrapping my head around rvalue references and C++11 goods.
No, they have different types.
Implementation-defined, but there’s unlikely to be any difference.
Lifetime extension works the same for rvalue and lvalue references. But the rule is more complicated than “the temporary value lifetime matches the reference lifetime”.
Probably not. I certainly wouldn’t go looking for places to use it. If you have a use for it, it had better be such an significant improvement to justify the use of such an unusual combination and the confusion that will ensue when people read it in the future.