Namespaces
Variants

std:: nothrow

From cppreference.net
< cpp ‎ | memory ‎ | new
Utilities library
Memory management library
( exposition only* )
Allocators
Uninitialized memory algorithms
Constrained uninitialized memory algorithms
Memory resources
Uninitialized storage (until C++20)
( until C++20* )
( until C++20* )
( until C++20* )

Garbage collector support (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
Definido en el encabezado <new>
(1)
struct nothrow_t { } ;
(hasta C++11)
struct nothrow_t { explicit nothrow_t ( ) = default ; } ;
(desde C++11)
extern const std:: nothrow_t nothrow ;
(2)

std::nothrow_t es un tipo de clase vacío utilizado para eliminar la ambigüedad entre las sobrecargas de las funciones de asignación con y sin lanzamiento de excepciones. std::nothrow es una constante del mismo.

Ejemplo

#include <iostream>
#include <new>
int main()
{
    try
    {
        while (true)
        {
            new int[100000000ul];   // throwing overload
        }
    }
    catch (const std::bad_alloc& e)
    {
        std::cout << e.what() << '\n';
    }
    while (true)
    {
        int* p = new(std::nothrow) int[100000000ul]; // non-throwing overload
        if (p == nullptr)
        {
            std::cout << "Allocation returned nullptr\n";
            break;
        }
    }
}

Salida:

std::bad_alloc
Allocation returned nullptr

Informes de defectos

Los siguientes informes de defectos que modifican el comportamiento se aplicaron retroactivamente a los estándares publicados anteriormente de C++.

DR Applied to Behavior as published Correct behavior
LWG 2510 C++11 el constructor por defecto no era explícito, lo que podía llevar a ambigüedad hecho explícito

Véase también

funciones de asignación
(función)