与g ++ 6.2不同的异常说明符

Big*_*awg 9 c++ g++ exception throw

有人可以向我解释为什么这个代码不能用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);
Run Code Online (Sandbox Code Playgroud)

Pal*_*lec 5

您使用的是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();
Run Code Online (Sandbox Code Playgroud)

已在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;
Run Code Online (Sandbox Code Playgroud)

参考链接.