Home >Backend Development >C++ >Do Const Rvalue References Have Any Practical Use in C ?
Const Rvalue References: A Rare but Useful Concept
The question of whether rvalue references to const (const Foo&&) have any practical use has puzzled some programmers. It would seem intuitive that they would not, but let's delve deeper into this topic and explore potential applications.
According to the C 0x draft, const rvalue references can be beneficial in certain scenarios. For instance, in the following code snippet used by the draft itself:
template <class T> void ref(const T&&) = delete; template <class T> void cref(const T&&) = delete;
These overloads ensure that the other ref(T&) and cref(const T&) functions do not bind to rvalues, preventing unexpected behavior.
Upon checking the official C 11 standard (N3290), the same declarations can be found in section 20.8 Function objects [function.objects]/p2:
template <class T> void ref(const T&&) = delete; template <class T> void cref(const T&&) = delete;
The latest publicly available post-C 11 draft (N3485) also includes these declarations in the same section. These examples demonstrate that const rvalue references have been deemed useful enough to be included in the C language standard. While their use cases may be specific, they provide a valuable construct for certain programming situations.
The above is the detailed content of Do Const Rvalue References Have Any Practical Use in C ?. For more information, please follow other related articles on the PHP Chinese website!