C ++ 11 rvalue reference questions
I'm trying to understand the new features of C ++ 11 and I have two questions regarding rvalue references.
-
I have not seen examples where functions return rvalue references (other than
std::move
). Are there cases where it makes sense to do this? E. g.,int&& foo(...)
. -
Are there cases where it makes sense to have rvalue references to const, either when declaring local variables or as function arguments? E. g.,
foo(int const&& i)
.
source to share
1) I have not seen examples where functions return rvalue references (except
std::move
). Are there cases where it makes sense to do this?
Aside from library functions that don't do more than cast their argument to an rvalue reference (such as std::move
and optionally std::forward
) and return that argument, I don't think you'll see this very often.
2) Are there cases where it makes sense to have rvalue references on
const
?
Little. In fact, almost none. Rvalue references are used when you want to bind to the object you want to navigate to, and since you cannot navigate out of the object const
(since movement changes the state of the object you are navigating from and const
promises vice versa), it makes no sense to have an rvalue reference to const
. Also note that lvalue references const
can also bind to r-values. There are several use cases for this .
source to share