【发布时间】:2012-05-04 09:54:48
【问题描述】:
我对@987654323@ 规范的案例特别感兴趣,它似乎随着 GCC 4.7 的引入而散布在 C++11 标准库中。在这种情况下,检测编译器版本就足够了;这是生成可移植代码的最佳机制吗?
#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)
...
};
【问题讨论】:
-
Boost.Config 将为不支持
noexcept规范的编译器定义宏BOOST_NO_NOEXCEPT。如果您担心 GCC 以外的编译器的可移植性,我建议让 Boost 为您完成艰苦的工作。 -
@ildjam Gar,如果不是最近的话!
-
你可以使用
#if defined(__has_feature) && __has_feature(cxx_noexcept)并且保证不会在没有实现它的编译器上使用 noexcept。另一方面,您可能无法在 确实 支持它的编译器上利用 noexcept,因为它们不支持功能检查宏...(AFAIK __has_feature 仅适用于 Clang)跨度> -
@bames53 :
__has_feature没有标准化,它只是 Clang 特定的扩展。 -
@ildjarn 确实它没有标准化,但它应该是事实上的标准。