Home » C++ » Do rvalue references to const have any use?

Do rvalue references to const have any use?

Posted by: admin November 30, 2017 Leave a comment

Questions:

I guess not, but I would like to confirm. Is there any use for const Foo&&, where Foo is a class type?

Answers:

They are occasionally useful. The draft C++0x itself uses them in a few places, for example:

template <class T> void ref(const T&&) = delete;
template <class T> void cref(const T&&) = delete;

The above two overloads ensure that the other ref(T&) and cref(const T&) functions do not bind to rvalues (which would otherwise be possible).

Update

I’ve just checked the official standard N3290, which unfortunately isn’t publicly available, and it has in 20.8 Function objects [function.objects]/p2:

template <class T> void ref(const T&&) = delete;
template <class T> void cref(const T&&) = delete;

Then I checked the most recent post-C++11 draft, which is publicly available, N3485, and in 20.8 Function objects [function.objects]/p2 it still says:

template <class T> void ref(const T&&) = delete;
template <class T> void cref(const T&&) = delete;

Questions:
Answers:

They are allowed and even functions ranked based on const, but since you can’t move from const object referred by const Foo&&, they aren’t useful.

Questions:
Answers:

I can’t think of a situation where this would be useful directly, but it might be used indirectly:

template<class T>
void f(T const &x) {
  cout << "lvalue";
}
template<class T>
void f(T &&x) {
  cout << "rvalue";
}

template<class T>
void g(T &x) {
  f(T());
}

template<class T>
void h(T const &x) {
  g(x);
}

The T in g is T const, so f‘s x is an T const&&.

It is likely this results in a comile error in f (when it tries to move or use the object), but f could take an rvalue-ref so that it cannot be called on lvalues, without modifying the rvalue (as in the too simple example above).