std::nothrow

定义于头文件 <new>
extern const std::nothrow_t nothrow;

std::nothrowstd::nothrow_t 类型的常量,用于消歧义抛出与不抛出分配函数的重载。

示例

#include <iostream>
#include <new>
 
int main()
{
    try {
        while (true) {
            new int[100000000ul];   // 抛出重载
        }
    } catch (const std::bad_alloc& e) {
        std::cout << e.what() << '\n';
    }
 
    while (true) {
        int* p = new(std::nothrow) int[100000000ul]; // 不抛出重载
        if (p == nullptr) {
            std::cout << "Allocation returned nullptr\n";
            break;
        }
    }
}

输出:

std::bad_alloc
Allocation returned nullptr

参阅

用于选择不抛出分配函数的标签类型
(类)
分配函数
(函数)

版本历史

  • (当前 | 先前 2017年5月17日 (三) 00:08Fruderica讨论 | 贡献. . (995字节) (-282). . (撤销)
  • 当前 | 先前 2014年10月26日 (日) 17:56P12bot讨论 | 贡献 . . (1,277字节) (0). . (Fix some translations) (撤销)
  • 当前 | 先前 2013年7月2日 (二) 09:04P12bot讨论 | 贡献 . . (1,277字节) (-68). . (Use {{lc}}. Update links. Various fixes.) (撤销)
  • 当前 | 先前 2012年11月2日 (五) 15:56P12bot讨论 | 贡献 . . (1,345字节) (+241). . (r2.7.3) (机器人添加:de, en, es, fr, it, ja, pt, ru) (撤销)
  • 当前 | 先前 2012年10月27日 (六) 03:26P12讨论 | 贡献 . . (1,104字节) (0). . (1个修订: Translate from the English version) (撤销)
  • 当前 | 先前 2012年10月26日 (五) 06:00TranslationBot讨论 | 贡献. . (1,104字节) (-18). . (Translated from the English version using Google Translate) (撤销)
  • 当前 | 先前 2012年10月25日 (四) 14:09P12讨论 | 贡献 . . (1,122字节) (0). . (1个修订: Translate from the English version) (撤销)
  • 当前 | 先前) 2012年10月25日 (四) 12:00TranslationBot讨论 | 贡献. . (1,122字节) (+1,122). . (Translated from the English version using Google Translate)