【发布时间】:2011-05-16 00:25:56
【问题描述】:
我有一个异常类如下:
#include <exception>
struct InvalidPathException : public std::exception
{
explicit InvalidPathException() {}
const char* what() const;
};
const char*
InvalidPathException::what() const {
return "Path is not valid";
}
在 GCC 4.4 下使用 -Wall -std=c++0x 编译时
错误:更宽松的抛出说明符 '虚拟常量字符* InvalidPathException::what() const'
错误:覆盖 'virtual const char* std::exception::what() const throw ()'
也非常正确,因为我覆盖了std::exception 的what() 方法,该方法确实具有throw() 异常说明符。但正如人们经常be informed 一样,我们不应该使用异常说明符。据我了解,它们是deprecated in C++11,但显然还没有在 GCC 中使用 -std=c++0x。
所以我现在对最好的方法感兴趣。在我正在开发的代码中,我确实关心性能,因此担心throw() 经常提到的开销,但实际上这种开销如此严重吗?我是否认为我只会在实际调用 what() 时遭受它,这只会在抛出这样的异常之后(同样对于从 std::exception 继承的其他方法都具有 throw() 说明符)?
或者,有没有办法解决 GCC 给出的这个错误?
【问题讨论】:
-
如果您只是将异常用于异常情况,而不是用于一般程序流,那么您不必担心抛出(或异常说明符)的性能开销。我注意到了与您相同的情况,我只是添加了说明符。
标签: c++ gcc c++11 exception-specification