我正在使用提升 1.57。我需要在我未广泛使用的专有编译器中禁用异常支持。当我需要使用boost::smart_ptr
时,以下步骤就像一个魅力:
使用以下
user.hpp
文件禁用 C++11 支持(使用 编译-DBOOST_USER_CONFIG="<user.hpp>"
):#define BOOST_HAS_NRVO #define BOOST_NO_COMPLETE_VALUE_INITIALIZATION #define BOOST_NO_CXX11_AUTO_DECLARATIONS #define BOOST_NO_CXX11_AUTO_MULTIDECLARATIONS #define BOOST_NO_CXX11_CHAR16_T #define BOOST_NO_CXX11_CHAR32_T #define BOOST_NO_CXX11_CONSTEXPR #define BOOST_NO_CXX11_DECLTYPE #define BOOST_NO_CXX11_DECLTYPE_N3276 #define BOOST_NO_CXX11_DEFAULTED_FUNCTIONS #define BOOST_NO_CXX11_DELETED_FUNCTIONS #define BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS #define BOOST_NO_CXX11_FINAL #define BOOST_NO_CXX11_FUNCTION_TEMPLATE_DEFAULT_ARGS #define BOOST_NO_CXX11_LAMBDAS #define BOOST_NO_CXX11_LOCAL_CLASS_TEMPLATE_PARAMETERS #define BOOST_NO_CXX11_NOEXCEPT #define BOOST_NO_CXX11_NULLPTR #define BOOST_NO_CXX11_RANGE_BASED_FOR #define BOOST_NO_CXX11_RAW_LITERALS #define BOOST_NO_CXX11_REF_QUALIFIERS #define BOOST_NO_CXX11_RVALUE_REFERENCES #define BOOST_NO_CXX11_SCOPED_ENUMS #define BOOST_NO_CXX11_STATIC_ASSERT #define BOOST_NO_CXX11_TEMPLATE_ALIASES #define BOOST_NO_CXX11_UNICODE_LITERALS #define BOOST_NO_CXX11_UNIFIED_INITIALIZATION_SYNTAX #define BOOST_NO_CXX11_USER_DEFINED_LITERALS #define BOOST_NO_CXX11_VARIADIC_MACROS #define BOOST_NO_CXX11_VARIADIC_TEMPLATES #define BOOST_NO_SFINAE_EXPR #define BOOST_NO_TWO_PHASE_NAME_LOOKUP
通知 boost 库不要使用异常:
-DBOOST_NO_EXCEPTIONS -DBOOST_EXCEPTION_DISABLE
导致以下编译行:
MyCOMPILER -DBOOST_USER_CONFIG="<user.hpp>" -DBOOST_NO_EXCEPTIONS -DBOOST_EXCEPTION_DISABLE -c Foo.cpp -o Foo.o
那么下面这段代码就编译成功了:
#include <iostream>
#include <string>
#include <boost/shared_ptr.hpp>
#include <boost/smart_ptr/make_shared.hpp>
namespace boost {
void throw_exception(std::exception const& e) {
std::cerr << "Fake exception: " << e.what() << "\n";
std::exit(255);
}
}
class Foo {
private:
boost::shared_ptr<int> ptr;
public:
bool bar(const std::string file_name) {
ptr = boost::make_shared<int>();
return true;
}
};
但是当我尝试将上述方法与以下源代码一起使用时,编译出错了:
#include <iostream>
#include <string>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/ini_parser.hpp>
namespace boost {
void throw_exception(std::exception const& e) {
std::cerr << "Fake exception: " << e.what() << "\n";
std::exit(255);
}
}
class Foo {
public:
bool bar(const std::string file_name) {
boost::property_tree::ptree prop_tree;
boost::property_tree::read_ini(file_name, prop_tree);
return !prop_tree.empty();
}
};
例如导致以下错误:
"boost\boost/optional/optional.hpp", line 1047: Error: #312: no suitable user-defined conversion from "boost::bad_optional_access" to "const std::exception" exists
throw_exception(bad_optional_access());
^
"boost\boost/optional/optional.hpp", line 1055: Error: #312: no suitable user-defined conversion from "boost::bad_optional_access" to "const std::exception" exists
throw_exception(bad_optional_access());
^
"boost\boost/type_index/stl_type_index.hpp", line 138: Error: #312: no suitable user-defined conversion from "std::runtime_error" to "const std::exception" exists
boost::throw_exception(std::runtime_error("Type name demangling failed"));
^
"boost\boost/property_tree/ini_parser.hpp", line 168: Error: #540: support for exception handling is disabled; use --exceptions to enable
try {
^
最后的问题:
为什么在第一种情况下一切正常,但在第二种情况下不行?如何boost::property_tree
在没有异常支持的情况下正确编译?这甚至可能吗?