std::move_if_noexcept

Da cppreference.com.
< cpp‎ | utility

 
 
Utilità libreria
Tipo di supporto (basic types, RTTI, type traits)
Gestione della memoria dinamica
La gestione degli errori
Programma di utilità
Funzioni variadic
Data e ora
Funzione oggetti
initializer_list(C++11)
bitset
hash(C++11)
Gli operatori relazionali
Original:
Relational operators
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
rel_ops::operator!=
rel_ops::operator>
rel_ops::operator<=
rel_ops::operator>=
Coppie e tuple
Original:
Pairs and tuples
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
pair
tuple(C++11)
piecewise_construct_t(C++11)
piecewise_construct(C++11)
Swap, in avanti e spostare
Original:
Swap, forward and move
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
swap
forward(C++11)
move(C++11)
move_if_noexcept(C++11)
declval(C++11)
 
Elemento definito nell'header <utility>
template<class T >

typenamestd::conditional<  
    !std::is_nothrow_move_constructible<T>::value&&std::is_copy_constructible<T>::value,
    const T&,
    T&&

>::type move_if_noexcept(T& x);
(dal C++11)
move_if_noexcept ottiene un riferimento rvalue al suo argomento se il suo costruttore mossa non generare eccezioni, ottiene comunque un riferimento lvalue al suo argomento. In genere viene utilizzato per combinare la semantica si muovono con la garanzia di una forte eccezione.
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.
Ad esempio, di tanto in tanto std::vector::resize alloca nuovo deposito e poi si sposta o elementi copie da vecchio magazzino a nuovo storage. Se si verifica un'eccezione durante questa operazione, std::vector::resize disfa tutto quello che ha fatto a questo punto, che è possibile solo se std::move_if_noexcept è stato usato per decidere se utilizzare la costruzione spostamento o copia costruzione.
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.

Indice

[modifica]Parametri

x -
l'oggetto da spostare o copiare
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.

[modifica]Valore di ritorno

std::move(x)x o, a seconda delle garanzie di eccezione.
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.

[modifica]Eccezioni

noexcept specification:  
noexcept
  (dal C++11)

[modifica]Esempio

#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);}

Output:

Non-throwing move constructor called Throwing copy constructor called

[modifica]Complessità

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

[modifica]Vedi anche

(C++11)
avanti una funzione argomento
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.

(funzione di modello)[modifica]
(C++11)
ottiene un riferimento 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.

(funzione di modello)[modifica]
close