std:: nothrow
From cppreference.net
C++
Utilities library
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Memory management library
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Low level memory management
| Functions | ||||
|
(C++11)
|
||||
| Classes | ||||
|
(C++11)
|
||||
|
(C++17)
|
||||
| Types | ||||
| Objects | ||||
|
nothrow
|
||||
|
(C++20)
|
||||
| Object access | ||||
|
(C++17)
|
|
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
Diesen Code ausführen
#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) |