与g++ 6.2不同的异常说明符
Different exception specifier with g++ 6.2
谁能给我解释一下为什么这段代码不能在g++ 6.2.0版本下编译,但在clang++ 3.9.0-svn274438-1和icpc 16.0.2版本下可以正常编译
$ cat wtf.cpp
#include <cstdio>
#include <new>
void *operator new(std::size_t) throw(std::bad_alloc);
void *operator new(std::size_t) throw (std::bad_alloc) { void *p; return p; }
$ g++-6 wtf.cpp -c
wtf.cpp: In function ‘void* operator new(std::size_t)’:
wtf.cpp:4:7: error: declaration of ‘void* operator new(std::size_t) throw (std::bad_alloc)’ has a different exception specifier
void *operator new(std::size_t) throw (std::bad_alloc) { void * p; return p; }
^~~~~~~~
wtf.cpp:3:7: note: from previous declaration ‘void* operator new(std::size_t)’
void *operator new(std::size_t) throw(std::bad_alloc);
您使用的是c++ 11或更高版本吗?
c++ 98中原始的new()操作符声明
throwing:
void* operator new (std::size_t size) throw (std::bad_alloc);
nothrow:
void* operator new (std::size_t size, const std::nothrow_t& nothrow_value) throw();
placement:
void* operator new (std::size_t size, void* ptr) throw();
在c++ 11中更改为使用noexcept关键字:
throwing:
void* operator new (std::size_t size);
nothrow:
void* operator new (std::size_t size, const std::nothrow_t& nothrow_value) noexcept;
placement:
void* operator new (std::size_t size, void* ptr) noexcept;
引用链接。
在GCC 6中,c++的默认模式已更改为 c++ 14。直到 GCC 5 是 C + + 98 。
operator new
声明在c++ 11中略有变化。这与抛出异常规范在c++ 11中被弃用以及nothrow
声明的引入有关:
-
throw (std::bad_alloc)
已省略 -
throw()
替换为nothrow
-std
参数指定您所针对的c++标准,如下所示:
$ g++-6 -std=c++98 wtf.cpp -c
相关文章:
- 激励'inline'说明符的真实世界示例?
- 处理多个异常集合的C++方法
- 我在c++代码中生成了一个运行时#3异常
- 孤立代码块在结构中引发异常
- C++中的赋值发生,尽管右侧出现异常
- 缺少类型说明符,显式类型为"缺少错误"
- 从构造函数抛出异常时如何克服内存泄漏
- 异常属于C++中的线程还是进程
- 当类定义不可见时捕获异常
- 引发异常:读取访问冲突**dynamicArray**为0x1118235.发生
- 为什么异常不退出程序?
- 为什么重载运算符上的异常说明符'<<'不适用于任何 std::ostream 对象,但对库中定义的运算符不起作用?
- 构造函数C++异常说明符
- 为什么当我在使用空 throw() 说明符指定的函数中抛出异常时没有调用 std::unexpected()
- 需要接受教育:声明"void encrypt(char*, int)"具有不同的异常说明符"
- 如何为析构函数指定nothrow异常说明符
- 标记异常之前的 C++ 预期类型说明符'…'
- 拆分定义和声明时,定义不同的异常说明符
- 与g++ 6.2不同的异常说明符
- C++17异常说明符类型系统将如何工作