Home >Backend Development >C++ >When Are `const Foo&&` Rvalue References Useful?

When Are `const Foo&&` Rvalue References Useful?

DDD
DDDOriginal
2024-12-09 19:00:22582browse

When Are `const Foo&&` Rvalue References Useful?

Rvalue References to Const: A Practical Perspective

Despite the common belief that rvalue references to const (const Foo&&) hold no utility, there are indeed practical applications for them, as demonstrated by the draft C 0x itself.

The C 0x draft employs such references in various instances to prevent unwanted binding of rvalues to other ref(T&) and cref(const T&) functions. These functions are specifically designed to accept only lvalues (references to objects), and rvalue references to const ensure that rvalues (references to temporaries) are not mistakenly bound.

Recently, upon reviewing the official standard N3290 (not publicly accessible), we discovered the following line in 20.8 Function objects [function.objects]/p2:

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

Further examination of the most recent post-C 11 draft, N3485 (publicly available), confirms the persistence of these lines in 20.8 Function objects [function.objects]/p2.

These examples illustrate the usefulness of rvalue references to const in certain scenarios, particularly when ensuring the correct behavior of function overloads and preventing unintended binding of rvalues.

The above is the detailed content of When Are `const Foo&&` Rvalue References Useful?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn