c++ - 编译期间 boost xml 序列化失败

标签 c++ xml serialization boost

我正在使用 boost::icl::interval_map 来维护城市的区间搜索树及其高温和低温。我想将 interval_map 序列化为一个文件。

该代码适用于文本和二进制序列化,但 xml 序列化无法编译。这是我得到的错误

root@ubuntu16:~/workspace/CPPCodes/interval-tree/city# make
g++ -o main  -std=c++14 -g -ggdb -O0 main.cpp -lboost_serialization
In file included from /usr/include/boost/mpl/aux_/na_assert.hpp:23:0,
                 from /usr/include/boost/mpl/arg.hpp:25,
                 from /usr/include/boost/mpl/placeholders.hpp:24,
                 from /usr/include/boost/archive/basic_binary_iprimitive.hpp:52,
                 from /usr/include/boost/archive/binary_iarchive_impl.hpp:21,
                 from /usr/include/boost/archive/binary_iarchive.hpp:20,
                 from main.cpp:4:
/usr/include/boost/archive/basic_xml_oarchive.hpp: In instantiation of ‘void boost::archive::basic_xml_oarchive<Archive>::save_override(T&, int) [with T = const boost::icl::interval_map<long unsigned int, boost::container::flat_set<City> >; Archive = boost::archive::xml_oarchive]’:
/usr/include/boost/archive/detail/interface_oarchive.hpp:63:9:   required from ‘Archive& boost::archive::detail::interface_oarchive<Archive>::operator<<(T&) [with T = const boost::icl::interval_map<long unsigned int, boost::container::flat_set<City> >; Archive = boost::archive::xml_oarchive]’
main.cpp:186:8:   required from here
/usr/include/boost/archive/basic_xml_oarchive.hpp:99:9: error: no matching function for call to ‘assertion_failed(mpl_::failed************ boost::serialization::is_wrapper<const boost::icl::interval_map<long unsigned int, boost::container::flat_set<City> > >::************)’
         BOOST_MPL_ASSERT((serialization::is_wrapper< T >));
         ^
/usr/include/boost/mpl/assert.hpp:83:5: note: candidate: template<bool C> int mpl_::assertion_failed(typename mpl_::assert<C>::type)
 int assertion_failed( typename assert<C>::type );
     ^
/usr/include/boost/mpl/assert.hpp:83:5: note:   template argument deduction/substitution failed:
/usr/include/boost/archive/basic_xml_oarchive.hpp:99:9: note:   cannot convert ‘mpl_::assert_arg<boost::serialization::is_wrapper<const boost::icl::interval_map<long unsigned int, boost::container::flat_set<City> > > >(0u, 1)’ (type ‘mpl_::failed************ boost::serialization::is_wrapper<const boost::icl::interval_map<long unsigned int, boost::container::flat_set<City> > >::************’) to type ‘mpl_::assert<false>::type {aka mpl_::assert<false>}’
         BOOST_MPL_ASSERT((serialization::is_wrapper< T >));
         ^
/usr/include/boost/archive/basic_xml_iarchive.hpp: In instantiation of ‘void boost::archive::basic_xml_iarchive<Archive>::load_override(T&, int) [with T = boost::icl::interval_map<long unsigned int, boost::container::flat_set<City> >; Archive = boost::archive::xml_iarchive]’:
/usr/include/boost/archive/xml_iarchive.hpp:103:51:   required from ‘void boost::archive::xml_iarchive_impl<Archive>::load_override(T&, int) [with T = boost::icl::interval_map<long unsigned int, boost::container::flat_set<City> >; Archive = boost::archive::xml_iarchive]’
/usr/include/boost/archive/detail/interface_iarchive.hpp:60:9:   required from ‘Archive& boost::archive::detail::interface_iarchive<Archive>::operator>>(T&) [with T = boost::icl::interval_map<long unsigned int, boost::container::flat_set<City> >; Archive = boost::archive::xml_iarchive]’
main.cpp:194:8:   required from here
/usr/include/boost/archive/basic_xml_iarchive.hpp:76:9: error: no matching function for call to ‘assertion_failed(mpl_::failed************ boost::serialization::is_wrapper<boost::icl::interval_map<long unsigned int, boost::container::flat_set<City> > >::************)’
         BOOST_MPL_ASSERT((serialization::is_wrapper< T >));
         ^
/usr/include/boost/mpl/assert.hpp:83:5: note: candidate: template<bool C> int mpl_::assertion_failed(typename mpl_::assert<C>::type)
 int assertion_failed( typename assert<C>::type );
     ^
/usr/include/boost/mpl/assert.hpp:83:5: note:   template argument deduction/substitution failed:
/usr/include/boost/archive/basic_xml_iarchive.hpp:76:9: note:   cannot convert ‘mpl_::assert_arg<boost::serialization::is_wrapper<boost::icl::interval_map<long unsigned int, boost::container::flat_set<City> > > >(0u, 1)’ (type ‘mpl_::failed************ boost::serialization::is_wrapper<boost::icl::interval_map<long unsigned int, boost::container::flat_set<City> > >::************’) to type ‘mpl_::assert<false>::type {aka mpl_::assert<false>}’
         BOOST_MPL_ASSERT((serialization::is_wrapper< T >));
         ^
Makefile:7: recipe for target 'main' failed
make: *** [main] Error 1

BOOST_MPL_ASSERT 在编译期间失败

// Anything not an attribute and not a name-value pair is an
// error and should be trapped here.
template<class T>
void save_override(T & t, BOOST_PFTO int)
{
    // If your program fails to compile here, its most likely due to
    // not specifying an nvp wrapper around the variable to
    // be serialized.
    BOOST_MPL_ASSERT((serialization::is_wrapper< T >));
    this->detail_common_oarchive::save_override(t, 0);
}

评论表明我没有在要序列化的成员周围使用 NVP 包装器。但是,我认为,我对所有成员变量都使用了 BOOST_SERIALIZATION_NVPboost::serialization::make_nvp。不确定发生了什么。

代码推送到这里:https://github.com/prasad-joshi/CPPCodes/tree/xml_failure/interval-tree/city

最佳答案

您的问题部分重复 assertion_failed when using Boost Serialization with xml_oarchive , 我认为。通过更改 oa << tree 修复编译错误至 oa << BOOST_SERIALIZATION_NVP(tree) .我得到的结果输出存档是有意义的:

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE boost_serialization>
<boost_serialization signature="serialization::archive" version="14">
<tree class_id="0" tracking_level="0" version="0">
    <sz>1</sz>
    <INTERVAL class_id="1" tracking_level="0" version="0">
        <bb>2</bb>
        <l>28</l>
        <u>39</u>
    </INTERVAL>
    <SET class_id="2" tracking_level="0" version="0">
        <count>1</count>
        <item_version>0</item_version>
        <item class_id="3" tracking_level="0" version="0">
            <city_>cpur</city_>
            <low_>28</low_>
            <high_>39</high_>
        </item>
    </SET>
</tree>

关于c++ - 编译期间 boost xml 序列化失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43398330/

相关文章:

c# - c# 容器中的 push_back 操作

C++ 继承获取错误

php - 如何在 PHP 中回显 XML 文件

python - 将现有模块更新到 Odoo 12 中的最新版本

xml - 在 XSD 中定义混合元素允许 "nesting"

java - Vaadin 中图表配置的序列化

c# - [DataMember] 如何与 Image 一起使用?

c# - 如何防止使用 Newtonsoft JSON 序列化 DependencyObjects 上的 DependencyObjectType 信息?

c++ - 使用 std::acos 从 std::cos(angle) 恢复精确角度

c++ - 运算符方法是否占用 C++ 对象中的内存?