std::move_if_noexcept
Da cppreference.com.
![]() | Questa pagina è stata tradotta in modo automatico dalla versione in ineglese della wiki usando Google Translate. La traduzione potrebbe contenere errori e termini strani. Muovi il puntatore sopra al testo per vedere la versione originale. Puoi aiutarci a correggere gli gli errori. Per ulteriori istruzioni clicca qui. |
Elemento definito nell'header <utility> | ||
template<class T > typenamestd::conditional< | (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.
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.
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.
You can help to correct and verify the translation. Click here for instructions.
[modifica]Eccezioni
[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.
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) |
(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) |