Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 带有g+的不同异常说明符+;6.2_C++_Exception_G++_Throw - Fatal编程技术网

C++ 带有g+的不同异常说明符+;6.2

C++ 带有g+的不同异常说明符+;6.2,c++,exception,g++,throw,C++,Exception,G++,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) {

有人能给我解释一下为什么这段代码不是用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);
$cat wtf.cpp
#包括
#包括
void*运算符new(std::size\u t)throw(std::bad\u alloc);
void*操作符new(std::size_t)抛出(std::bad_alloc){void*p;返回p;}
$g++-6 wtf.cpp-c
wtf.cpp:在函数“void*运算符new(std::size_t)”中:
wtf.cpp:4:7:错误:“void*运算符new(std::size_t)throw(std::bad_alloc)”的声明具有不同的异常说明符
void*操作符new(std::size_t)抛出(std::bad_alloc){void*p;返回p;}
^~~~~~~~
wtf.cpp:3:7:注:来自上一个声明“void*operator new(std::size_t)”中
void*运算符new(std::size\u t)throw(std::bad\u 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;

< P> <强> GCC 6 < /强> C++的默认模式必须<<强> C++ 14 < /强>。在gcc5之前,它是C++98

在C++11中,
运算符new
声明已略有更改。它与以下事实有关:抛出异常规范是,并且引入了
nothrow
声明:

  • throw(std::bad\u alloc)
    已被省略
  • throw()
    替换为
    nothrow
<>最佳向后兼容性,您应该指定使用C++代码> -STD < /C> >参数的C++标准,如:

$ g++-6 -std=c++98 wtf.cpp -c 

通过添加
-std=c++03
,g++可以很好地编译。但这并不能解释为什么我会收到这个错误信息,对吗?我认为,@BigDawg-
throw()
noexcept
具有类似的效果,但是
throw(std::bad_alloc)
与不指定抛出的异常完全不同。