C中的内联throw()方法

C中的内联throw()方法,第1张

概述我试图定义一个非常简单的异常类.因为它非常简单,我只想将它保存在.h文件中,但编译器不喜欢throw().代码: #include <exception>#include <string>class PricingException : public virtual std::exception{private: std::string msg;public: 我试图定义一个非常简单的异常类.因为它非常简单,我只想将它保存在.h文件中,但编译器不喜欢throw().代码:
#include <exception>#include <string>class PricingException : public virtual std::exception{private:    std::string msg;public:        PricingException(std::string message) : msg(message) {}        const char* what() const throw() { return msg.c_str(); }        ~PricingException() throw() {}};

GCC给出以下错误:

/home/ga/dev/CppGroup/MonteCarlo/PricingException.h:13: error: expected unqualifIEd-ID before ‘{’ token/home/ga/dev/CppGroup/MonteCarlo/PricingException.h:14: error: expected unqualifIEd-ID before ‘{’ token

对于带有throw()的行.知道怎么解决吗?

编辑

我试图删除有问题的方法的主体,即

virtual ~PricingException() throw();// {}

现在我得到更奇怪的错误信息:

/home/ga/dev/CppGroup/MonteCarlo/PricingException.h:14: error: looser throw specifIEr for ‘virtual PricingException::~PricingException()’/usr/include/c++/4.5/exception:65: error:   overrIDing ‘virtual std::exception::~exception() throw ()’

它只是忽略了我的throw说明符!

解决方法 尝试使用C 0x语法,g 4.5可能已经足够支持它了:
const char* what() const noexcept { return msg.c_str(); }

但是,这应该不重要(来自3242草案,[除了.spec]部分的措辞:

Two exception-specifications are compatible if:

both are non-throwing (see below),regardless of their form, both have the form noexcept(constant-Expression) and the constant-Expressions are equivalent, one exception-specification is a noexcept-specification allowing all exceptions and the other is of the form throw(type-ID-List),or both are dynamic-exception-specifications that have the same set of adjusted types.

.

If a virtual function has an exception-specification,all declarations,including the deFinition,of any function that overrIDes that virtual function in any derived class shall only allow exceptions that are allowed by the exception-specification of the base class virtual function.

.

A function with no exception-specification or with an exception-specification of the form noexcept(constant-Expression) where the constant-Expression yIElds false allows all exceptions. An exception-specification is
non-throwing if it is of the form throw(),noexcept,or noexcept(constant-Expression) where the constant-Expression yIElds true. A function with a non-throwing exception-specification does not allow any exceptions.

因此,尝试更新的g版本,这些更改可能会更完整地实现.

总结

以上是内存溢出为你收集整理的C中的内联throw()方法全部内容,希望文章能够帮你解决C中的内联throw()方法所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1257005.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-07
下一篇 2022-06-07

发表评论

登录后才能评论

评论列表(0条)

保存