std::nothrow
cppreference.net에서
(cpp/memory/new/nothrow t에서 넘어옴)
| 헤더 <new>
|
||
struct nothrow_t {};
|
에 정의됨 (1) | (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 | 적용 대상 | 게시된 동작 | 올바른 동작 |
|---|---|---|---|
| LWG 2510 | C++11 | 기본 생성자가 non-explicit였으며, 이는 모호성을 초래할 수 있었음 | explicit로 변경됨 |
참고 항목
|
할당 함수
(함수) |