Home >Backend Development >C++ >When Are Const Reference Values Useful in C ?

When Are Const Reference Values Useful in C ?

Linda Hamilton
Linda HamiltonOriginal
2024-12-22 05:41:09966browse

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!

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