全局关闭C++新运算符异常
Turn off C++ new operator exceptions globally
是否有任何方法可以全局关闭new
运算符的异常?如果不止一个,哪一个最好?
我试过这个,但我真的不确定:
#include <new>
using std::nothrow;
我试着在谷歌上搜索"使用std::nothrow;",但没有结果。
我正在使用MSVC 2010。
我当然知道new (std::nothrow) myClass();
否。这会破坏很多代码,例如标准标头中的代码,而标准标头确实依赖于new
抛出。
C++委员会意识到在一个名称下标准化几十种几乎兼容的语言所带来的危险,如果只有5种这样的选项,你就已经有32种不兼容的语言了。
#define NEW1(type, ...) new (std::nothrow) type(__VA_ARGS__)
#define NEW(type, size, ...) new (std::nothrow) type[size](__VA_ARGS__)
//用法:
int *a=NEW1(int), //single non-initialized int
*b=NEW1(int, 42), //single int with value 42
*c=NEW(int, 42); //array of ints made of 42 elements
delete a;
delete b;
delete[] c;
相关文章:
- 对 r 值使用移动赋值运算符时的异常
- iostream运算符重载的C++异常规范
- 未找到匹配的运算符删除;如果初始化引发异常,内存将不会被释放
- 在重载运算符>>部分中收到异常
- std::vector::emplace() 真的在面对抛出移动构造函数/赋值运算符时提供了强大的异常保证吗?
- 动态指针引用数组由三元运算符返回值,但有异常
- 运算符重载时未处理<<异常
- 为什么重载运算符上的异常说明符'<<'不适用于任何 std::ostream 对象,但对库中定义的运算符不起作用?
- c++运算符=工作异常(函数内部输出正常,但返回后错误)
- C++隐式生成的赋值运算符的异常安全性
- C++:异常的运算符重载
- 为什么在 std::vector 中使用索引超出范围的运算符 [] 时没有出现异常?
- 如何在类数组中的运算符 [] 中捕获异常
- 是否可以从三元运算符引发异常
- 运算符重载未处理异常
- std::bad_alloc C++调用运算符 = 方法时引发的异常
- GCC 前缀增量运算符在将值传递给函数时行为异常
- 具有空异常规范的运算符new在分配返回0时调用构造函数
- std::istream 运算符异常重置/未抛出
- 全局关闭C++新运算符异常