VC ++ 2008/2010:throw()或__declspec(nothrow)是更好的选择吗? [英] VC++ 2008/2010: is throw() or __declspec(nothrow) a better choice?

查看:94
本文介绍了VC ++ 2008/2010:throw()或__declspec(nothrow)是更好的选择吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用VC ++ 2008和2010时,最好使用哪个标记来表示函数不会引发异常:

When using VC++ 2008 and 2010, which marker is better to use to indicate a function won't throw exceptions:

  • throw()(C ++标准)
  • __declspec(nothrow)(MS扩展名)
  • throw() (C++ standard)
  • __declspec(nothrow) (MS extension)

我阅读了一些较早的论坛讨论,其中一些人说使用throw()可能实际上会迫使编译器生成其他代码来捕获异常,以防函数抛出异常(针对标记).他们的建议不是使用throw(),而是使用__declspec(nothrow),因为编译器实际上可以使用它进行优化.

I read a few older forum discussions where people said that using throw() may actually force the compiler to generate additional code to catch exceptions in case the function does throw (against the marker). Their advice is not to use throw() but use __declspec(nothrow) instead, since the compiler can actually use it for optimization.

我进行了一些搜索,但无法给出真正有用的结果.据我了解,Boost库建议不要在此处使用它们. __declspec(nothrow)是非标准的C ++,因此,如果MS实现异常规范,则在throw()行为可能更改的情况下,它将保持相同的工作方式.

I did some searches but I couldn't come up with really useful results. From how I understand, the Boost library advises against using them here. __declspec(nothrow) is non-standard C++, so if MS implements exception specifications, it'll keep working the same way while the throw() behavior may change.

推荐答案

标准格式为noexcept,但是VC ++ 2008和2010不支持此格式.

The standard form is noexcept, but VC++ 2008 and 2010 don't support this.

我个人会使用一个定义为throw()(或什至什么都没有)的宏,直到编译器开始支持C ++ 11 noexcept,然后再对其进行更改.

Personally, I'd use a macro, defined as throw() (or maybe even nothing) until compilers start supporting C++11 noexcept, and then change it.

这篇关于VC ++ 2008/2010:throw()或__declspec(nothrow)是更好的选择吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆