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::forward function

The std::forward function

17 Sep 2021

The std::forward function as the std::move function aims at implementing move semantics in C++. The function takes a forwarding reference. According to the T template parameter, std::forward identifies whether an lvalue or an rvalue reference has been passed to it and returns a corresponding kind of reference. std::forward helps to implement perfect forwarding. This mechanism implies that objects passed to the function as lvalue expressions should be copied, and objects passed to the function as rvalue expressions should be moved.

If you assign an rvalue reference to some ref variable, then ref is a named entity. Therefore, its category is lvalue, despite the fact that ref is an rvalue reference. Therefore, for ref copy semantics is used instead of move semantics.

The std::forward function solves this problem. Let's consider the following example:

template <typename T>
void foo(T &&arg)
{
  std::vector<int> var = arg;
  ....
}

std::vector<int> vect(1'000'000, 1);
foo(std::move(vect));

That's what happens in the code fragment:

  • The std::move function is called. It returns an rvalue reference to vect.
  • This reference is passed via a forwarding link to the instance of the foo function as the arg argument. arg is an lvalue object of the rvalue reference type.
  • The var variable is initialized. It calls the copy constructor. vector vect is copied element-by-element to the var variable.

Now let's use std::forward:

template <typename T>
void foo(T &&arg)
{
  std::vector<int> var = std::forward<T>(arg);
  ....
}

std::vector<int> vect(1'000'000, 1);
foo(std::move(vect));

Here's what happens:

  • The std::move function is called. It returns an rvalue reference to vect.
  • This reference is passed via a forwarding link to the instance of the foo function as the arg argument. arg is an lvalue object of the rvalue reference type.
  • The std::forward function is called. It returns an xvalue object with the rvalue reference type.
  • The var variable is initialized. It calls the move constructor. vector vect is moved to the var variable.
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