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

When Are Const Reference Values Useful in C ?

Linda Hamilton
Release: 2024-12-22 05:41:09
Original
1009 people have browsed it

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;
Copy after login
Copy after login

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;
Copy after login
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template