如何使用谷物或一般序列化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);
}
};
答案 0 :(得分:2)
正如您所发现的那样,可以通过添加谷物中包含的std::chrono::minute
来找到对std::chrono::duration
的支持,这实际上是cereal/types/chrono.hpp
。