BOOST_NO_EXCEPTIONS是否保证与-fno-exceptions的兼容性?

inf*_*inf 16 c++ boost exception

我想和Boost.Filesystem一起使用-fno-exceptions.根据Boost.Filesystem文档,它声明它支持BOOST_NO_EXCEPTIONS宏.

但是,以下代码段:

#define BOOST_NO_EXCEPTIONS

#include <boost/filesystem.hpp>

int main() {}
Run Code Online (Sandbox Code Playgroud)

编译:

g ++ -fno-exceptions boost_test.cpp

给出错误:

/.../boost/filesystem/operations.hpp:在构造函数'boost :: filesystem :: filesystem_error :: filesystem_error(const string&,boost :: system :: error_code)':/.../boost/filesystem/operations .hpp:84:16:错误:禁用异常处理,使用-fexceptions启用catch(...){m_imp_ptr.reset(); }

我在Mac OSX上使用gcc 5和boost 1.57进行编译(也在类似的ubuntu设置上进行了测试).

我想知道我的理解BOOST_NO_EXCEPTIONS是否正确,因为它应该涵盖使用-fno-exceptions或是否只是在那boost::throw_exception部分?

Han*_*ant 9

好吧,"不"是明显的答案,g ++无法处理filesystem_error类.boost/filesystem/config.hpp中有一个humdinger:

//  throw an exception  ----------------------------------------------------------------//
//
//  Exceptions were originally thrown via boost::throw_exception().
//  As throw_exception() became more complex, it caused user error reporting
//  to be harder to interpret, since the exception reported became much more complex.
//  The immediate fix was to throw directly, wrapped in a macro to make any later change
//  easier.

#define BOOST_FILESYSTEM_THROW(EX) throw EX
Run Code Online (Sandbox Code Playgroud)

此宏在libs/filesystem/src/operations.cpp中广泛使用以引发异常.这是一个表演者.

Fwiw,你的示例程序似乎只能在clang和MSVC++中正确编译,他们只在后端抱怨必须发出异常处理代码,g ++在它的前端做.clang/msvc ++没有对此示例代码提出任何抱怨,因为之前已经发出了异常处理代码,在构建boost库时也是如此.

这表明您的方法存在另一个严重问题,您最初可能在没有-fno-exceptions的情况下构建了boost.不好.

  • Upvoted,非常有趣!另外,TIL"humdinger". (2认同)