user2052436
user2052436

Reputation: 4765

C++11 rvalue reference questions

I am trying to understand new features of C++11, and I have two questions regarding rvalue references.

  1. I have not seen examples when functions return rvalue references (except std::move). Are there cases when it makes sense to do so? E. g., int&& foo(...).

  2. Are there cases when it makes sense to have rvalue references to const, either when declaring local variables or as function arguments? E. g., foo(int const&& i).

Upvotes: 3

Views: 196

Answers (1)

Andy Prowl
Andy Prowl

Reputation: 126412

1) I have not seen examples when functions return rvalue references (except std::move). Are there cases when it makes sense to do so?

Apart from library functions that are not doing much more than casting their argument to an rvalue reference (such as std::move and, when appropriate, std::forward) and returning that argument, I don't believe you will meet that very often.

2) Are there cases when it makes sense to have rvalue references to const?

Not many. Almost none, actually. Rvalue references are used when you want to bind to an object you eventually want to move from, and since you cannot move from a const object (because moving alters the state of the object you move from, and const promises the opposite), it makes little sense to have an rvalue reference to const. Also notice, that lvalue references to const can bind to rvalues as well. There are a few use cases for that though.

Upvotes: 5

Related Questions