In the realm of C programming, an intriguing question arises: Do rvalue references to const class types serve any purpose? While one might initially assume they are redundant, a closer examination reveals that they have occasional utility.
The draft C 0x language standard provides an example of their application. Two function templates, ref and cref, are defined as follows:
template <class T> void ref(const T&&) = delete; template <class T> void cref(const T&&) = delete;
These overloads serve a critical purpose: preventing the other ref(T&) and cref(const T&) functions from being invoked with rvalues. This would be possible if the rvalue references to const were not defined.
Update:
Upon further examination of the official C standard (N3290), the same function templates appear in section 20.8 [function.objects]/p2. Additionally, the most recent post-C 11 draft (N3485) also includes these templates in the same location.
The above is the detailed content of When Are Const Rvalue References Useful in C ?. For more information, please follow other related articles on the PHP Chinese website!