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)
Definiert in Header <new>
(1)
struct nothrow_t { } ;
(bis C++11)
struct nothrow_t { explicit nothrow_t ( ) = default ; } ;
(seit C++11)
extern const std:: nothrow_t nothrow ;
(2)

std::nothrow_t ist ein leerer Klassentyp, der zur Unterscheidung der Überladungen von werfenden und nicht-werfenden Allokationsfunktionen verwendet wird. std::nothrow ist eine Konstante davon.

Beispiel

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

Ausgabe:

std::bad_alloc
Allocation returned nullptr

Fehlerberichte

Die folgenden verhaltensändernden Fehlerberichte wurden rückwirkend auf zuvor veröffentlichte C++-Standards angewendet.

DR Angewendet auf Verhalten wie veröffentlicht Korrektes Verhalten
LWG 2510 C++11 der Standardkonstruktor war non-explicit, was zu Mehrdeutigkeit führen konnte explicit gemacht

Siehe auch

Allokierungsfunktionen
(Funktion)