Home >Backend Development >C++ >When Are Const Reference Values Useful in C ?
Are Const Reference Values of Any Use?
Skepticism regarding the utility of const Foo&&, where Foo represents a class type, is not unfounded. However, contrary to the initial assumption, const reference values do have occasional applications.
The C 0x draft provides a notable example with the following declarations:
template <class T> void ref(const T&&) = delete; template <class T> void cref(const T&&) = delete;
These overloads prevent ref(T&) and cref(const T&) from binding to rvalues, which would otherwise be possible.
Furthermore, the official C standard N3290, contains a similar set of declarations:
template <class T> void ref(const T&&) = delete; template <class T> void cref(const T&&) = delete;
These examples demonstrate the usefulness of const reference values in preventing accidental binding to rvalues.
The above is the detailed content of When Are Const Reference Values Useful in C ?. For more information, please follow other related articles on the PHP Chinese website!