Unicorn with delicious cookie
Nous utilisons des cookies pour améliorer votre expérience de navigation. En savoir plus
Accepter
to the top
>
>
>
The std::move function

The std::move function

17 Sep 2021

The std::move function aims at implementing move semantics. The function receives a forwarding reference to an object and returns an rvalue reference to this object. One of the possible implementations of std::move looks as follows:

template<typename T>
std::remove_reference_t<T>&& move(T&& x) 
{
  return static_cast<std::remove_reference_t<T>&&>(x);
}

lvalue and rvalue references can be passed to the std::move function, and an rvalue reference must be obtained as a result. Therefore, to denote the return type, we first use the std::remove_reference_t trait, that simplifies the template type T to a usual non-reference type. Then we explicitly add '&&'. Let's take a look at the following code fragment:

std::vector<int> &vec1 = DoSomeCalculations();
std::vector<int> vec2 = std::move(vec1);

Here's what we have here:

  • An instance of the std::move function is instantiated with template type T = std::vector<int> & .
  • This instance takes an argument of the std::vector<int> & type. When trying to form an argument as an rvalue reference to std::vector<int> &, the compiler uses reference collapse, and the result argument type is equivalent to std::vector<int> &. This is an lvalue reference to vector<int>.
  • The std::remove_reference_t trait returns the std::vector<int> type simplified to a non-reference type. '&&' is added to std::vector<int>. As a result, the return type of std::vector<int> && is an rvalue reference to vector<int>.

Now let's consider the case when an rvalue reference is passed to std::move:

std::vector<int> &&vec1 = DoAnotherCalculations();
std::vector<int> vec2 = std::move(vec1);

Here's what happens:

  • An instance of the move function with template type T = std::vector<int> is instantiated.
  • This instance receives an argument of the std::vector<int> && type. It is an rvalue reference on vector<int>.
  • The std::remove_reference_t trait returns the std::vector<int> type simplified to a non-reference type. '&&' is added to std::vector<int>. As a result, the return type of std::vector<int> && is an rvalue reference to vector<int>.

The std::move call results in an xvalue. This means that the resources of such an object can be reused. If it's possible, move semantics (not the copy semantics) is applied for an xvalue expression. For example, if the result of the std::move function is assigned to some obj object of the type type, then the move assignment operator is called (if it is defined for type).

Additional links:

Popular related articles

S'abonner

Comments (0)

close comment form
close form

Remplissez le formulaire ci‑dessous en 2 étapes simples :

Vos coordonnées :

Étape 1
Félicitations ! Voici votre code promo !

Type de licence souhaité :

Étape 2
Team license
Enterprise licence
** En cliquant sur ce bouton, vous déclarez accepter notre politique de confidentialité
close form
Demandez des tarifs
Nouvelle licence
Renouvellement de licence
--Sélectionnez la devise--
USD
EUR
* En cliquant sur ce bouton, vous déclarez accepter notre politique de confidentialité

close form
La licence PVS‑Studio gratuit pour les spécialistes Microsoft MVP
close form
Pour obtenir la licence de votre projet open source, s’il vous plait rempliez ce formulaire
* En cliquant sur ce bouton, vous déclarez accepter notre politique de confidentialité

close form
I want to join the test
* En cliquant sur ce bouton, vous déclarez accepter notre politique de confidentialité

close form
check circle
Votre message a été envoyé.

Nous vous répondrons à


Si l'e-mail n'apparaît pas dans votre boîte de réception, recherchez-le dans l'un des dossiers suivants:

  • Promotion
  • Notifications
  • Spam