Home > Backend Development > C++ > How does `std::move()` achieve the conversion of lvalues to rvalues in C ?

How does `std::move()` achieve the conversion of lvalues to rvalues in C ?

Linda Hamilton
Release: 2024-11-21 08:14:09
Original
518 people have browsed it

How does `std::move()` achieve the conversion of lvalues to rvalues in C  ?

How does std::move Transform Expressions into Rvalues?

std::move() facilitates the conversion of expressions into rvalues (right value references) to enable move semantics. However, the implementation in the MSVC standard library can be perplexing.

The implementation is as follows:

template<class _Ty> inline
typename tr1::_Remove_reference<_Ty>::_Type&amp;&amp;
move(_Ty&amp;&amp; _Arg)
{ // forward _Arg as movable
    return ((typename tr1::_Remove_reference<_Ty>::_Type&amp;&amp;)_Arg);
}
Copy after login

Let's demonstrate how this works:

Object obj1;
Object obj2 = std::move(obj1); // _Ty&amp;&amp; _Arg binds to obj1
Copy after login

Upon calling std::move(), the _Arg reference parameter binds to the lvalue obj1. However, since binding an rvalue reference to an lvalue is not directly allowed, one might expect a cast to an rvalue reference, such as (Object&&), to be required.

Delving further into the implementation of std::remove_reference clarifies matters:

template<class _Ty>
struct _Remove_reference
{ // remove reference
    typedef _Ty _Type;
};

template<class _Ty>
struct _Remove_reference<_Ty&amp;>
{ // remove reference
    typedef _Ty _Type;
};

template<class _Ty>
struct _Remove_reference<_Ty&amp;&amp;>
{ // remove rvalue reference
    typedef _Ty _Type;
};
Copy after login

This implementation reveals that remove_reference transforms T& into T, T&& into T, and T&&& into T. Therefore, for our case, where obj1 is an lvalue of type Object, the resulting function becomes:

Object&amp;&amp; move(Object&amp; arg)
{
    return static_cast<Object&amp;&amp;>(arg);
}
Copy after login

To elaborate further:

  • When move is invoked with an rvalue (e.g., std::move(Object())), T becomes Object and the resulting function is:
Object&amp;&amp; move(Object&amp;&amp; arg)
{
    return static_cast<Object&amp;&amp;>(arg);
}
Copy after login

The cast to Object&& is necessary because named rvalue references are treated as lvalues, while implicit conversion from lvalue to rvalue reference is disallowed.

  • When move is invoked with an lvalue, as in our earlier example, T becomes Object&, and the resulting function is:
Object&amp;&amp; move(Object&amp; &amp;&amp; arg)
{
    return static_cast<Object&amp;&amp;>(arg);
}
Copy after login

Here, C 11's reference collapsing rules come into play and allow the expression Object& && to be interpreted as Object&, an lvalue reference, which can indeed bind to lvalues. Thus, the final function transforms its argument into an rvalue reference and returns it.

In conclusion, std::move() utilizes std::remove_reference to enable the transformation of both lvalues and rvalues into rvalues, facilitating move semantics in C .

The above is the detailed content of How does `std::move()` achieve the conversion of lvalues to rvalues 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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template