Unicorn with delicious cookie
Nous utilisons des cookies pour améliorer votre expérience de navigation. En savoir plus
Accepter
to the top
>
>
>
Move semantics

Move semantics

17 Sep 2021

Move semantics is a set of semantic rules and tools of the C++ language. It was designed to move objects, whose lifetime expires, instead of copying them. The data is transferred from one object to another. In most cases, the data transfer does not move this data physically in memory. It helps to avoid expensive copying.

Move semantics was introduced in the C++11 standard. To implement it, rvalue references, move constructors, and the move assignment operator were added. Also, some functions were added to the standard template library (STL) to support move semantics. For example, std::move and std::forward.

Uses of move semantics

Let's say you need to write a Swap template function that accepts two objects of the same type and swaps them. The function may be implemented as follows:

template <typename T>
void Swap(T &lhs, T &rhs)
{
  T t = lhs;
  lhs = rhs;
  rhs = t;
}

Everything seems great – the function can work with two objects of the same type and swap them. However, such implementation has a significant drawback. Let's take a look at the following code fragment:

std::vector<int> arrA(1'000'000, 0);
std::vector<int> arrB(1'000'000, 1);
Swap(arrA, arrB);

Two objects of the std::vector<int> type are created. Each contains 1'000'000 elements. Then the Swap function swaps them. The std::vector class template contains a non-trivial copy constructor that has the following functions:

  • performs dynamic memory allocation to the desired number of elements;
  • makes a deep copy of the elements from the passed std::vector.

As a result, we have 3'000'000 copies of int type objects. The situation may get even worse if std::vector is instantiated by a non-trivially copied type.

Move semantics helps to get rid of unnecessary copying. To use it, we need to convert an object to an rvalue reverence. Thus, we tell the compiler that it can move the object.

#include <type_traits>

template <typename T>
void Swap(T &lhs, T &rhs) noexcept
{
  using rvalue_ref = typename std::remove_reference<T>::type &&;
  T t = static_cast<rvalue_ref>(lhs);
  lhs = static_cast<rvalue_ref>(rhs);
  rhs = static_cast<rvalue_ref>(t);
}

In case of std::vector, its non-trivial constructor/move operator swaps pointers to dynamic memory, thus eliminating expensive memory allocation and elements copying.

To simplify the process of coding when moving objects, the std::move function was introduced into the standard library. This function casts the object passed to it to an rvalue reference:

#include <utility>

template <typename T>
void Swap(T &lhs, T &rhs) noexcept
{
  T t = std::move(lhs);
  lhs = std::move(rhs);
  rhs = std::move(t);
}
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