std::move_if_noexcept
De cppreference.com
|
|
This page has been machine-translated from the English version of the wiki using Google Translate.
The translation may contain errors and awkward wording. Hover over text to see the original version. You can help to fix errors and improve the translation. For instructions click here. |
| Defined in header <utility>
|
||
| template< class T > typename std::conditional< |
(ya que C + +11) | |
move_if_noexcept obtiene una referencia a su argumento rvalue si su constructor movimiento no lanza excepciones, si no obtiene una referencia de valor-para su argumento. Se suele utilizar para combinar semántica se mueven con garantía excepción fuerte .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.
Por ejemplo, de vez en cuando std::vector::resize asigna almacenamiento nuevo y luego se mueve o elementos de almacenamiento de copias de antiguo al nuevo almacenamiento. Si se produce una excepción durante esta operación, std::vector::resize deshace todo lo que hizo a este punto, lo cual sólo es posible si std::move_if_noexcept se utiliza para decidir si se debe utilizar la construcción de movimiento o construcción copia .
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.
Contenido |
[editar] Parámetros
| x | - | el objeto que se mueve o se copia
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. |
[editar] Valor de retorno
std::move(x) o x, en función de las garantías de excepción .
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.
[editar] Excepciones
[editar] Ejemplo
#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
[editar] Complejidad
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.
[editar] Ver también
| (C++11) |
reenvía una función argumento 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. (función de plantilla) |
| (C++11) |
obtiene una referencia de valor- 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. (función de plantilla) |