Home >Backend Development >C++ >Why Doesn't an Rvalue Reference Variable Match an Rvalue Reference Function Signature?

Why Doesn't an Rvalue Reference Variable Match an Rvalue Reference Function Signature?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-12-10 22:46:11810browse

Why Doesn't an Rvalue Reference Variable Match an Rvalue Reference Function Signature?

Why an Rvalue Reference Variable Not an Rvalue?

Let's consider two overloaded functions f:

void f(T&&); // Overload #1
void f(T&);  // Overload #2

Within function g:

void g(T&& t) { 
  f(t);  // Calls Overload #2
}

Surprisingly, Overload #2 is called since t is considered an lvalue, despite the first overload having an rvalue reference signature (T&&).

How can an rvalue reference signature fail to match a call passing an rvalue?

Understanding C Rules for Rvalues

Rvalues are generally variables without names that will lose their names shortly. T&& t has the identifier t, so it's an lvalue. This makes sense because rvalues should be short-lived and impossible to refer to later.

However, T&& is the type rvalue reference. It can only bind to rvalues without involving a static cast, but it is still an lvalue of type rvalue reference. Its rvalue reference nature only matters during its initialization and decltype use. Otherwise, it acts as an lvalue of reference type.

Static Cast and Reference Extension

std::move(t) returns an rvalue reference by performing a static cast on t. This allows the temporary value to have reference lifetime extension when it is bound to a reference outside of a constructor.

The C standard defines explicit rules for handling these scenarios. Rvalue references and const references can bind to rvalues. Implicit moves occur when returning a named value from a function, when a value has no name, or when a function explicitly returns an rvalue reference.

In addition, T&& may not always be an rvalue reference. If T is X& or X const&, reference collapsing converts T&& to X& or X const&. Finally, T&& can act as a "forwarding reference" in type deduction, depending on the argument's type.

The above is the detailed content of Why Doesn't an Rvalue Reference Variable Match an Rvalue Reference Function Signature?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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