可能重复:
我特别感兴趣的是noexcept规范,它似乎在C++11标准库中随处可见,介绍了GCC 4.7。在这种情况下,检测编译器版本就足够了;这是生成可移植代码的最佳机制吗?
#include <system_error>
class server_error_category : public std::error_category
{
public:
virtual const char* name () const { ... }
//
// fails beginning with gcc4.7,
// looser throw specifier ...
// overriding 'virtual const char* std::error_category::name() const noexcept (true)
...
};发布于 2012-04-23 19:07:41
如果您只想检查noexcept是否在编译时(而不是预处理时)应用于特定的方法,那么只需使用noexcept操作符即可。
noexcept(std::declval<std::error_category>().name())
// return 'true' if the expression is 'noexcept'然后,除使用语法外,还可以有条件地使该方法为you。
virtual const char* name () const
noexcept(noexcept(std::declval<std::error_category>().name()))
{ ... }您无法检查库是否支持‘t,除非是预处理时间。版本检查是唯一的方法。
但无论如何,你还是可以加入noexcept的。即使基类不是noexcept,子类的重写也是有效的。
virtual const char* name () const noexcept { ... }
// valid even in 4.6.(请注意,Boost.Config并没有真正的帮助,因为在语言中,从4.6开始就支持noexcept,但是库的使用出现在4.7中。)
https://stackoverflow.com/questions/10286447
复制相似问题