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)
定義済みヘッダー <new>
(1)
struct nothrow_t { } ;
(C++11以前)
struct nothrow_t { explicit nothrow_t ( ) = default ; } ;
(C++11以降)
extern const std:: nothrow_t nothrow ;
(2)

std::nothrow_t は、例外送出版と非例外送出版の アロケーション関数 のオーバーロードを区別するために使用される空のクラス型です。 std::nothrow はその定数です。

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

出力:

std::bad_alloc
Allocation returned nullptr

不具合報告

以下の動作変更の欠陥報告書は、以前に公開されたC++規格に対して遡及的に適用されました。

DR Applied to Behavior as published Correct behavior
LWG 2510 C++11 デフォルトコンストラクタが非explicitであり、曖昧性を引き起こす可能性があった explicitに変更

関連項目

メモリ確保関数
(関数)