new_nothrow_t — std::nothrow_t

new_nothrow_t — std::nothrow_t

std::nothrow_tnew가 실패했을 때 예외를 던지지 않는 버전을 요청하기 위한 태그 타입이에요. <new> 헤더에 제공돼요.

std::nothrow 상수를 new에 전달하면, 할당 실패 시 예외를 던지는 대신 nullptr을 반환하는 nothrow 배치 new가 호출돼요.

출처: cppreference

본문

std::nothrow_t는 독립적인 빈 태그 타입이에요.

struct nothrow_t {};

이 타입의 인스턴스인 전역 상수 std::nothrow가 제공돼요. new (std::nothrow) T처럼 사용하면, 할당이 실패해도 예외를 던지지 않고 nullptr을 반환하는 배치 new가 선택돼요.

int* p = new (std::nothrow) int[1000];
if (p == nullptr) { /* 메모리 부족 처리 */ }

예외를 쓰고 싶지 않은 환경이나, 할당 실패를 널 체크로 처리하고 싶을 때 유용해요.

같이 보기

  • std::nothrow

더 알아보기 (Learn more)

cppreference