具有动态库的std :: chrono的谷物序列化问题

时间:2019-02-28 12:39:01

标签: c++11 serialization cereal

我在谷物库(http://uscilab.github.io/cereal/)上遇到问题。

我有一个共享库,我想使用谷物库序列化其中一个类。它具有std :: chrono

中的time_point成员

这是我在Event.h中对象代码的一部分

    #include <cereal/types/chrono.hpp>
    #include <cereal/types/string.hpp>

    class Event
    {
    private:
         std::string m_Id;
         std::chrono::high_resolution_clock::time_point m_StartTime;
    public:
        template<class Archive> void serialize(Archive & archive)
        {
          archive(m_Id, m_StartTime);
        }

库编译没有问题。然后,我想在可执行文件中使用我的库,在其中尝试序列化对象之一:

    #include "Event.h"
    #include <cereal/archives/json.hpp>
    cereal::JSONOutputArchive output(std::cout);
    output(API::Event());

此代码无法编译,并且抱怨time_point缺少序列化函数。如果我只打算序列化它编译的字符串。

构建错误输出:

[ 20%] Building CXX object CMakeFiles/plugin.dir/src/main.cpp.o
In file included from /cereal/include/cereal/types/memory.hpp:33:0,
             from main.cpp:7:
cereal/include/cereal/cereal.hpp: In instantiation of ‘ArchiveType& 
cereal::OutputArchive<ArchiveType, Flags>::processImpl(const T&) [with T = std::chrono::time_point<std::chrono::_V2::system_clock, std::chrono::duration<long int, std::ratio<1, 1000000000> > >; typename cereal::traits::detail::EnableIfHelper<(cereal::traits::has_invalid_output_versioning<T, ArchiveType>::value || ((! cereal::traits::is_output_serializable<T, ArchiveType>::value) && ((!(Flags & AllowEmptyClassElision)) || ((Flags & AllowEmptyClassElision) && (! std::is_empty<T>::value)))))>::type <anonymous> = (cereal::traits::detail::type)0; ArchiveType = cereal::JSONOutputArchive; unsigned int Flags = 0]’:
cereal/include/cereal/cereal.hpp:347:9:   required from ‘void cereal::OutputArchive<ArchiveType, Flags>::process(T&&) [with T = std::chrono::time_point<std::chrono::_V2::system_clock, std::chrono::duration<long int, std::ratio<1, 1000000000> > >&; ArchiveType = cereal::JSONOutputArchive; unsigned int Flags = 0]’
cereal/include/cereal/cereal.hpp:249:9:   required from ‘ArchiveType& cereal::OutputArchive<ArchiveType, Flags>::operator()(Types&& ...) [with Types = {std::chrono::time_point<std::chrono::_V2::system_clock, std::chrono::duration<long int, std::ratio<1, 1000000000> > >&}; ArchiveType = cereal::JSONOutputArchive; unsigned int Flags = 0]’
Event.h:66:16:   required from ‘void Event::serialize(Archive&) [with Archive = cereal::JSONOutputArchive]’
cereal/include/cereal/access.hpp:243:51:   required from ‘static decltype (t.serialize(ar)) cereal::access::member_serialize(Archive&, T&) [with Archive = cereal::JSONOutputArchive; T = Event; decltype (t.serialize(ar)) = void]’
cereal/include/cereal/cereal.hpp:400:33:   required from ‘ArchiveType& cereal::OutputArchive<ArchiveType, Flags>::processImpl(const T&) [with T = Event; typename cereal::traits::detail::EnableIfHelper<cereal::traits::has_member_serialize<T, ArchiveType>::value, (! cereal::traits::has_invalid_output_versioning<T, ArchiveType>::value), (cereal::traits::is_output_serializable<T, ArchiveType>::value && (cereal::traits::is_specialized_member_serialize<T, ArchiveType>::value || (! cereal::traits::is_specialized<T, ArchiveType>::value)))>::type <anonymous> = (cereal::traits::detail::type)0; ArchiveType = cereal::JSONOutputArchive; unsigned int Flags = 0]’
cereal/include/cereal/cereal.hpp:347:9:   required from ‘void cereal::OutputArchive<ArchiveType, Flags>::process(T&&) [with T = Event; ArchiveType = cereal::JSONOutputArchive; unsigned int Flags = 0]’
cereal/include/cereal/cereal.hpp:249:9:   required from ‘ArchiveType& cereal::OutputArchive<ArchiveType, Flags>::operator()(Types&& ...) [with Types = {Event}; ArchiveType = cereal::JSONOutputArchive; unsigned int Flags = 0]’
main.cpp:38:36:   required from here
cereal/include/cereal/cereal.hpp:462:9: error: static assertion failed: cereal could not find any output serialization functions for the provided type and archive combination. 

编辑:

我猜问题出在共享库中定义了序列化功能。

如果我只是编译一个具有time_point的测试类(而不是在我的项目中只是为了进行测试),则它会按预期工作。

1 个答案:

答案 0 :(得分:0)

它似乎是由于包含顺序的缘故。存档的包含必须在类型/时间的包含之前。因此正确的顺序是:

#include <cereal/archives/json.hpp>
#include "Event.h"
cereal::JSONOutputArchive output(std::cout);
output(API::Event());