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:
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.
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
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!