[ Web Proxy ]
URL:
Viewing: https://fr.cppreference.com/cpp/utility/move_if_noexcept [Back]  [Original]

std::move_if_noexcept cppreference.com
cppreference.com
Espaces de noms
Variantes

std::move_if_noexcept

De cppreference.com

<metanoindex/>

<tbody> </tbody>
Dclar dans l'en-tte <utility>
template< class T > typename std::conditional< !std::is_nothrow_move_constructible<T>::value && std::is_copy_constructible<T>::value, const T&, T&& >::type move_if_noexcept(T& x);
(depuis C++11)
move_if_noexcept obtient une rfrence rvalue son argument si son constructeur mouvement ne lvent pas d'exceptions, obtient par ailleurs une rfrence lvalue son argument. Il est gnralement utilis pour combiner smantique dplacer avec la garantie exception forte .
Original:
move_if_noexcept obtains an rvalue reference to its argument if its move constructor does not throw exceptions, otherwise obtains an lvalue reference to its argument. It is typically used to combine move semantics with strong exception guarantee.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Par exemple, l'occasion de stockage std::vector::resize alloue nouveau, puis se dplace ou d'lments de stockage des copies anciennes de stockage. Si une exception se produit lors de cette opration, std::vector::resize dfait tout ce qu'il a fait ce point, ce qui n'est possible que si std::move_if_noexcept a t utilis pour dcider s'il faut utiliser la construction de dplacement ou de la construction copie .
Original:
For example, std::vector::resize occasionally allocates new storage and then moves or copies elements from old storage to new storage. If an exception occurs during this operation, std::vector::resize undoes everything it did to this point, which is only possible if std::move_if_noexcept was used to decide whether to use move construction or copy construction.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

Paramtres

x -
l'objet dplacer ou copier
Original:
the object to be moved or copied
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

Retourne la valeur

std::move(x) ou x, en fonction des garanties d'exception .
Original:
std::move(x) or x, depending on exception guarantees.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

Exceptions

noexcept specification:  
<tbody> </tbody>
noexcept
   (depuis C++11)

Exemple

#include <iostream>
#include <utility>

struct Bad
{
    Bad() {}
    Bad(Bad&&)  // may throw
    {
        std::cout << "Throwing move constructor called\n";
    }
    Bad(const Bad&) // may throw as well
    {
        std::cout << "Throwing copy constructor called\n";
    }
};

struct Good
{
    Good() {}
    Good(Good&&) noexcept // will NOT throw
    {
        std::cout << "Non-throwing move constructor called\n";
    }
    Good(const Good&) {};
};

int main()
{
    Good g;
    Bad b;
    Good g2 = std::move_if_noexcept(g);
    Bad b2 = std::move_if_noexcept(b);
}

Rsultat :

Non-throwing move constructor called
Throwing copy constructor called

Complexit

Constante
Original:
Constant
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

Voir aussi

(C++11)
avant un argument de fonction
Original:
forwards a function argument
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(fonction gnrique) [edit]
(C++11)
obtient une rfrence rvalue
Original:
obtains an rvalue reference
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(fonction gnrique) [edit]

Web Proxy Viewer  |  New URL  |  Original Page