Espacios de nombres
Variantes
Acciones

std::move_if_noexcept

De cppreference.com
< cpp‎ | utility

 
 
Utilidades de la biblioteca
Escriba apoyo (basic types, RTTI, type traits)
Gestión de memoria dinámica
Gestión de errores
Programa de servicios públicos
Funciones variadic
Fecha y hora
Objetos Function
initializer_list(C++11)
bitset
hash(C++11)
Operadores relacionales
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>=
Pares y tuplas
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)
Cambiar, hacia adelante y mover
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)
 
Defined in header <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);
(desde 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.
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.

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.

[editar] Excepciones

noexcept specification:  
noexcept
  (desde C++11)

[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.

[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) [edit]
(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) [edit]