如何序列化 std::chrono::minutes

How do I serialize std::chrono::minutes

如何使用谷物或一般方式序列化 std::chrono::minutes?

我试过了

struct A {
    std::chrono::minutes m;

    template <class Archive>
    void serialize(Archive& ar) { 
        ar(m);
    }
};
int main()    
{
        A a;

        std::ostringstream os;
        cereal::JSONOutputArchive ar1(os);
        ar1(cereal::make_nvp("A", a));

        A result;
        std::istringstream is(os.str());
        cereal::JSONInputArchive ar2(is);
        ar2(cereal::make_nvp("A", result));

        if (a.m != result.m)
            std::cout << "error\n";
}

但在 Visual Studio 2015

时出现错误
1>   Types must either have a serialize function, load/save pair, or load_minimal/save_minimal pair (you may not mix these).
1>   Serialize functions generally have the following signature:
1>
1>   template<class Archive>
1>     void serialize(Archive & ar)
1>     {
1>       ar( member1, member2, member3 );
1>     }
1>
1>
1>  cereal.hpp(702): note: see reference to function template instantiation 'ArchiveType &cereal::InputArchive<ArchiveType,0>::processImpl<std::chrono::minutes,0>(const T &)' being compiled
1>          with
1>          [
1>              ArchiveType=cereal::JSONInputArchive,
1>              T=std::chrono::minutes
1>          ]

理想情况下,我想以一种可移植的方式进行序列化和反序列化。

我想我通过包含 cereal/types/chrono.hpp 解决了这个问题 并更改为

#include <cereal/types/chrono.hpp>
class A {
    std::chrono::minutes m;

    friend class cereal::access;

    template <class Archive>
    void serialize(Archive& ar)
    { 
        ar(m);
    }
};

如您所见,std::chrono::minute 的支持实际上是 std::chrono::duration,可以通过包含谷物中包含的 cereal/types/chrono.hpp 来找到。