关闭 RTTI 的简单类的 Boost 序列化 (-fno-rtti)

发布于 2024-12-11 16:55:36 字数 2799 浏览 0 评论 0原文

我正在尝试使用 boost 序列化来序列化具有普通旧数据类型的简单类。然而,我唯一的要求是我不能使用 RTTI。因此,我使用 gcc 4.4.1 for ARM Linux 和最新的 Boost 1.47 库通过 -fno-rtti 进行编译。

这是我的课程:

#include <boost/archive/xml_oarchive.hpp>
#include <boost/archive/xml_iarchive.hpp>

class libNemoMemento
{
    friend class boost::serialization::access;
    private:
        template<class Archive>
        void serialize(Archive & ar, const unsigned int version)
        {
            ar & BOOST_SERIALIZATION_NVP(temperature);
            ar & BOOST_SERIALIZATION_NVP(voltage);
            ar & BOOST_SERIALIZATION_NVP(bandwidth);
            ar & BOOST_SERIALIZATION_NVP(power);
        }
        int temperature;
        unsigned int voltage;
        unsigned int bandwidth;
        unsigned char power;
    public:
        libNemoMemento(void) {}
        virtual ~libNemoMemento(void) {}
};

我什至还没有实现实际的保存和加载功能(从查看 Boost 文档来看,它们似乎非常简单),但我已经收到以下编译器错误:

In file included from /home/me/prebuild/third-party/boost/include/boost/serialization/detail/shared_ptr_132.hpp:29,
                 from /home/me/third-party/boost/include/boost/serialization/shared_ptr_132.hpp:35,
                 from /home/me/third-party/boost/include/boost/archive/shared_ptr_helper.hpp:29,
                 from /home/me/third-party/boost/include/boost/archive/xml_iarchive.hpp:133,
                 from serialize_test.h:21,
                 from serialize_test.cpp:15:
/home/me/third-party/boost/include/boost/serialization/detail/shared_count_132.hpp: In member function 'virtual void* boost_132::detail::sp_counted_base_impl<P, D>::get_deleter(const std::type_info&)':
/home/me/third-party/boost/include/boost/serialization/detail/shared_count_132.hpp:274: error: cannot use typeid with -fno-rtti
In file included from /home/me/prebuild/third-party/boost/include/boost/serialization/shared_ptr_132.hpp:35,
                 from /home/me/prebuild/third-party/boost/include/boost/archive/shared_ptr_helper.hpp:29,
                 from /home/me/prebuild/third-party/boost/include/boost/archive/xml_iarchive.hpp:133,
                 from serialize_test.h:21,
                 from serialize_test.cpp:15:
/home/me/prebuild/third-party/boost/include/boost/serialization/detail/shared_ptr_132.hpp: In function 'D* boost_132::get_deleter(const boost_132::shared_ptr<U>&)':
/home/me/prebuild/third-party/boost/include/boost/serialization/detail/shared_ptr_132.hpp:465: error: cannot use typeid with -fno-rtti
make: *** [all] Error 1

所以..问题是,是否可以使用 boost 序列化来序列化这个简单的类而不使用 RTTI?我环顾四周,似乎可以使用一些 boost 宏和机制(#include < boost/serialization/extended_type_info_no_rtti.hpp > 暗示了这一点),但我是一个新的 Boost 用户,对如何使用非常无能为力继续。

PS:如果删除 -fno-rtti,我的代码可以正常编译。

I am trying to serialize a simple class with Plain-old-Data types using boost serialization. However, my only requirement is that I cannot use RTTI. Thus I am compiling with -fno-rtti using gcc 4.4.1 for ARM Linux with latest Boost 1.47 library.

So here is my class:

#include <boost/archive/xml_oarchive.hpp>
#include <boost/archive/xml_iarchive.hpp>

class libNemoMemento
{
    friend class boost::serialization::access;
    private:
        template<class Archive>
        void serialize(Archive & ar, const unsigned int version)
        {
            ar & BOOST_SERIALIZATION_NVP(temperature);
            ar & BOOST_SERIALIZATION_NVP(voltage);
            ar & BOOST_SERIALIZATION_NVP(bandwidth);
            ar & BOOST_SERIALIZATION_NVP(power);
        }
        int temperature;
        unsigned int voltage;
        unsigned int bandwidth;
        unsigned char power;
    public:
        libNemoMemento(void) {}
        virtual ~libNemoMemento(void) {}
};

I haven't even implemented the actual save and load functions yet (they seem to be pretty straightforward from looking at the Boost documentation) but I am already getting the following compiler errors:

In file included from /home/me/prebuild/third-party/boost/include/boost/serialization/detail/shared_ptr_132.hpp:29,
                 from /home/me/third-party/boost/include/boost/serialization/shared_ptr_132.hpp:35,
                 from /home/me/third-party/boost/include/boost/archive/shared_ptr_helper.hpp:29,
                 from /home/me/third-party/boost/include/boost/archive/xml_iarchive.hpp:133,
                 from serialize_test.h:21,
                 from serialize_test.cpp:15:
/home/me/third-party/boost/include/boost/serialization/detail/shared_count_132.hpp: In member function 'virtual void* boost_132::detail::sp_counted_base_impl<P, D>::get_deleter(const std::type_info&)':
/home/me/third-party/boost/include/boost/serialization/detail/shared_count_132.hpp:274: error: cannot use typeid with -fno-rtti
In file included from /home/me/prebuild/third-party/boost/include/boost/serialization/shared_ptr_132.hpp:35,
                 from /home/me/prebuild/third-party/boost/include/boost/archive/shared_ptr_helper.hpp:29,
                 from /home/me/prebuild/third-party/boost/include/boost/archive/xml_iarchive.hpp:133,
                 from serialize_test.h:21,
                 from serialize_test.cpp:15:
/home/me/prebuild/third-party/boost/include/boost/serialization/detail/shared_ptr_132.hpp: In function 'D* boost_132::get_deleter(const boost_132::shared_ptr<U>&)':
/home/me/prebuild/third-party/boost/include/boost/serialization/detail/shared_ptr_132.hpp:465: error: cannot use typeid with -fno-rtti
make: *** [all] Error 1

So.. the question is, is it possible to serialize this simple class using boost serialization without using RTTI?? I have looked around and it seems like it may be possible using some boost macros and machinery (#include < boost/serialization/extended_type_info_no_rtti.hpp > alludes to this) but I am a new Boost user and pretty clueless as to how to proceed.

PS: My code compiles fine if I remove -fno-rtti.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文