【问题标题】:boost::property_tree without exceptionsboost::property_tree 没有例外
【发布时间】:2015-10-02 04:57:33
【问题描述】:

我需要解析一些 INI 文件。为此,我尝试使用boost::property_tree,但在我的系统中不允许出现异常。

使用boost::property_tree时如何禁用异常支持?

如果没有办法做到这一点,非常感谢任何对其他库的建议。

在@sehe的回答之后,我尝试了这段代码,但没有成功:

#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();
      }
};

编译行代码使用以下参数:

-c -DBOOST_USER_CONFIG="<user.hpp>" -DBOOST_NO_EXCEPTIONS -DBOOST_EXCEPTION_DISABLE -I.\toolchain\boost -I.\include Foo.cpp

最后,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/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/property_tree/string_path.hpp", line 221: Error:  #312: no suitable user-defined conversion from "boost::property_tree::ptree_bad_path" to "const std::exception" exists
          BOOST_PROPERTY_TREE_THROW(ptree_bad_path("Path syntax error", *this));
          ^

"boost/property_tree/detail/ptree_implementation.hpp", line 78: Error:  #742: namespace "boost" has no actual member "iterator_core_access"
          friend class boost::iterator_core_access;
                              ^

PS:我没有使用广泛可用的标准编译器,但它使用上述相同的过程成功编译了boost::smart_ptr

【问题讨论】:

  • 简短回答 - 算了。 Boost 不是这里的答案。解析 ini 文件是微不足道的,property_tree 使错误处理策略不足使这变得非常复杂。开销是不值得的。

标签: c++ exception boost boost-propertytree


【解决方案1】:

您可以将 Boost Exception 配置为不使用异常:

您需要定义BOOST_NO_EXCEPTIONS 并进一步实现您自己的C 风格异常处理函数,例如

void throw_exception(std::exception const& e) {
    std::cerr << "Fake exception: " << e.what() << "\n";
    std::exit(255);
}

Live On Coliru

#define BOOST_NO_EXCEPTIONS
#include <iostream>
#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);
    }
}

using namespace boost::property_tree;

int main() {
    ptree pt;
    read_ini(std::cin, pt);

    write_ini(std::cout, pt.get_child("section5"));
}

编译

g++ -std=c++11 -O3 -Wall -pedantic main.cpp -fno-exceptions

打印出来

./test <<< "[section1]"
Fake exception: No such node (section5)

【讨论】:

  • 我这样做了,但现在我收到以下错误:“boost/property_tree/string_path.hpp”,第 221 行:错误:#312:没有合适的用户定义转换来自“boost::property_tree ::ptree_bad_path" 到 "const std::exception" 存在 BOOST_PROPERTY_TREE_THROW(ptree_bad_path("路径语法错误", *this));
  • @Valmir 看来您没有显示足够的代码。很可能您在那里偶然发现了一个错误,但如果没有最小的自包含代码示例,就无法判断
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-15
  • 2011-06-27
  • 2019-02-10
  • 2012-09-14
  • 1970-01-01
相关资源
最近更新 更多