Home > Article > Backend Development > Why Does C Use the Same Syntax (T&&) for Forwarding References and Rvalue References?
Why do universal references share the same syntax (T&&) as rvalue references? Isn't this confusing?
A universal reference (T&&) can deduce T as an "object type" or a "reference type." When passed an rvalue, T deduces to int, resulting in an int&& parameter. When passed an lvalue, T deduces to int&, collapsing to int& due to reference collapsing rules.
When T cannot be deduced (e.g., in the X::baz example), it's not a universal reference.
Using the same syntax for universal references and rvalue references streamlines template argument deduction and reference collapsing rules. The proposed tweak allows template parameters to be deduced as reference types, addressing the forwarding problem.
Alternative syntaxes, such as T&&&&&, T@, or T&42, were considered but rejected. They would have introduced confusion as the declarator T&&&@ could not represent an actual type (it would always refer to int&& or int&).
The current syntax aligns with the broader type system: when T is an lvalue reference, T&& is too. The rules are consistent and avoid unnecessary complexity.
The above is the detailed content of Why Does C Use the Same Syntax (T&&) for Forwarding References and Rvalue References?. For more information, please follow other related articles on the PHP Chinese website!