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 는 throwing 및 non-throwing allocation functions 의 오버로드를 구분하기 위해 사용되는 빈 클래스 타입입니다. 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 적용 대상 게시된 동작 올바른 동작
LWG 2510 C++11 기본 생성자가 non-explicit였으며, 이는 모호성을 초래할 수 있었음 explicit로 변경됨

참고 항목

할당 함수
(함수)