Home > Backend Development > C++ > What's the Difference Between Universal and Forwarding References in C ?

What's the Difference Between Universal and Forwarding References in C ?

DDD
Release: 2024-11-12 13:08:02
Original
666 people have browsed it

What's the Difference Between Universal and Forwarding References in C  ?

Demystifying Universal and Forwarding References

In C , the distinction between universal and forwarding references has been a subject of debate. To clarify this confusion, we explore their relationship and definitions.

The Essence of Universal References

Historically, universal references were introduced to describe template parameters that could accept rvalue references to cv-unqualified types. This allowed the template to deduce either a value or an lvalue reference based on the argument passed.

Forwarding References Defined

The term "forwarding reference" was introduced in the C standard (N4164) to address the lack of a specific term for this concept in C 11. A forwarding reference is formally defined as an rvalue reference to a cv-unqualified template parameter. If the argument is an lvalue, "lvalue reference to A" is substituted for type "A" during type deduction.

Universality and Forwarding: Same Coin, Different Sides

The terms "universal reference" and "forwarding reference" describe the same concept. They both refer to rvalue references to cv-unqualified template parameters that enable the deduction of either a value or an lvalue reference. The current C standard term is "forwarding reference," which is considered a more accurate and concise descriptor.

The Irrelevance of std::forward

It is crucial to note that the use of std::forward within a function body is unrelated to the concept of forwarding references. Forwarding references refer solely to the deduction of template types based on rvalue references to cv-unqualified parameters. Their behavior is independent of whether or not std::forward is subsequently invoked.

The above is the detailed content of What's the Difference Between Universal and Forwarding References 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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template