如何使用 Boost 序列化 std::chrono::duration?
How to serialize std::chrono::duration with Boost?
是否可以用 boost::serialization
序列化 std::chrono::duration
或特别是 std::chrono::milliseconds
?
发生以下错误:
"struct std::chrono::duration >’ has no member named ‘serialize"
我的模板需要这个-class:
#include <iostream>
#include <chrono>
#include <boost/serialization/serialization.hpp>
template<typename T>
class Field {
public:
// Default Constructor
Field() {}
Field(std::string name, T value) :
mName(name), mValue(value) {}
~Field() {}
const std::string& getName() const {
return mName;
}
T getValue() const {
return mValue;
}
private:
friend class boost::serialization::access;
template<typename Archive>
void serialize(Archive& archive, const unsigned int) {
archive & boost::serialization::make_nvp("Name", mName);
archive & boost::serialization::make_nvp("Value", mValue);
}
std::string mName;
T mValue;
};
BOOST_CLASS_IMPLEMENTATION( Field<int>, boost::serialization::object_serializable )
BOOST_CLASS_IMPLEMENTATION( Field<std::chrono::milliseconds>, boost::serialization::object_serializable )
我正在创建以下对象:
Field<std::chrono::milliseconds> mSimTime;
假设您有:
auto earlier = std::chrono::system_clock::now();
auto now = std::chrono::system_clock::now();
auto aLotOfMilliseconds= std::chrono::duration_cast<std::chrono::milliseconds>(
now - earlier);
// 在这里连载 int64_t.
那么在反序列化之后,你有:
std::chrono::duration<int64_t, std::milli> duration(aLotOfMilliseconds);
是否可以用 boost::serialization
序列化 std::chrono::duration
或特别是 std::chrono::milliseconds
?
发生以下错误:
"struct std::chrono::duration >’ has no member named ‘serialize"
我的模板需要这个-class:
#include <iostream>
#include <chrono>
#include <boost/serialization/serialization.hpp>
template<typename T>
class Field {
public:
// Default Constructor
Field() {}
Field(std::string name, T value) :
mName(name), mValue(value) {}
~Field() {}
const std::string& getName() const {
return mName;
}
T getValue() const {
return mValue;
}
private:
friend class boost::serialization::access;
template<typename Archive>
void serialize(Archive& archive, const unsigned int) {
archive & boost::serialization::make_nvp("Name", mName);
archive & boost::serialization::make_nvp("Value", mValue);
}
std::string mName;
T mValue;
};
BOOST_CLASS_IMPLEMENTATION( Field<int>, boost::serialization::object_serializable )
BOOST_CLASS_IMPLEMENTATION( Field<std::chrono::milliseconds>, boost::serialization::object_serializable )
我正在创建以下对象:
Field<std::chrono::milliseconds> mSimTime;
假设您有:
auto earlier = std::chrono::system_clock::now();
auto now = std::chrono::system_clock::now();
auto aLotOfMilliseconds= std::chrono::duration_cast<std::chrono::milliseconds>(
now - earlier);
// 在这里连载 int64_t.
那么在反序列化之后,你有:
std::chrono::duration<int64_t, std::milli> duration(aLotOfMilliseconds);