Home > Backend Development > C++ > How Do C 11 Reference Collapsing Rules Enable Perfect Forwarding?

How Do C 11 Reference Collapsing Rules Enable Perfect Forwarding?

Linda Hamilton
Release: 2024-12-26 22:27:13
Original
454 people have browsed it

How Do C  11 Reference Collapsing Rules Enable Perfect Forwarding?

Role of Reference Collapsing Rules in C 11

Reference collapsing rules play a crucial role in enabling perfect forwarding in C 11. Perfect forwarding allows functions to seamlessly accept and pass on parameters by value, lvalue reference, or rvalue reference.

Reference Collapsing Rules

The reference collapsing rules for C 11 are:

  • A& & & -> A&
  • A& & & -> A&
  • A& & & -> A&
  • A& & & & & -> A& &

Rationale for Reference Collapsing

These rules ensure that, regardless of how a parameter is passed (by lvalue, rvalue, or rvalue reference), it will always be "seen" by the receiving function as the desired type for perfect forwarding.

  • To Lvalue Reference: When passing an lvalue by lvalue reference (e.g., T&), the collapsing rule converts "& & &" to "&", providing the correct lvalue reference for binding to the receiver's non-const lvalue reference.
  • To Const Lvalue Reference: When passing a const lvalue by const lvalue reference (e.g., const T&), the collapsing rule ensures that the constness is preserved and the parameter is bound to the correct const lvalue reference in the receiver.
  • To Rvalue Reference: Passing an rvalue reference (e.g., T& &), regardless of the incoming value type, results in a collapse to "& &", which allows perfect forwarding of xvalues (temporary objects) and prvalues (function return values of type T& &).

Utilization in C 11 STL Utility Functions

STL utility functions such as std::move() and std::forward() utilize reference collapsing internally to facilitate perfect forwarding. For instance:

std::forward<> collapses "& &" to "&" for lvalue references and "& &" to "& &" for rvalue references, enabling Call to receive its parameter in the appropriate format.

Relationship with std::remove_reference

Utilities like std::remove_reference can be used to remove references from a type deduction (e.g., std::remove_reference returns T). However, it does not replace the need for reference collapsing rules in C 11. std::remove_reference simply removes the reference layers, while reference collapsing ensures that the forwarded parameter is received in the correct format for perfect forwarding.

The above is the detailed content of How Do C 11 Reference Collapsing Rules Enable Perfect Forwarding?. 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