2013-11-21 82 views
3

我想用boost :: serialize序列化一個包含std :: chrono :: system_clock :: time_point的類。boost :: serialize和std :: chrono :: system_clock :: time_point

template <class Archive> void serialize(Archive& ar, unsigned int version) { 
    ar & timePoint_; 
} 

但是我得到的錯誤:

錯誤1個錯誤C2039: '序列化':是不是成員「的std ::時辰:: time_point < _Clock> C:\ boost_1_54_0 \提升\序列化\ access.hpp 118

我該如何解決這個問題?

回答

0

我設法得到它使用下面的侵入功能(其中有timepoint_作爲一個成員函數的類)

template <class Archive> void serialize(Archive& ar, unsigned int version) { 
    char * ptr = reinterpret_cast<char *>(&timePoint_); 
    for(int i = 0; i < sizeof(timePoint_); ++i) 
     ar & ptr[i]; 
    /*snip*/ 
} 
0

使用

boost::serialization::make_binary_object(void * t, size_t size);

boost/serialization/binary_object.hpp工作wchich可像這樣使用:

std::chrono::system_clock::time_point tp; 
ar & boost::serialization::make_binary_object(&tp, sizeof(tp)); 
1

添加speci在序列化類聲明點之前std :: chrono :: time的alization可能會有所幫助。必須的boost ::系列化命名空間中聲明使用的

namespace boost { 
namespace archive { 

    namespace chrn = std::chrono; 

    template<class Archive, typename clock> 
    void load(Archive& ar, chrn::time_point<clock>& tp, unsigned) 
    { 
     chrn::milliseconds::rep millis; 
     ar & millis; 
     tp = chrn::time_point<clock>(chrn::milliseconds(millis)); 
    } 

    template<class Archive, typename clock> 
    void save(Archive& ar, chrn::time_point<clock> const& tp, unsigned) 
    { 
     chrn::milliseconds::rep millis = chrn::duration_cast<chrn::milliseconds>(tp.time_since_epoch()).count(); 
     ar & millis; 
    } 

    template<class Archive, typename clock> 
    inline void serialize(Archive & ar, std::chrono::time_point<clock>& tp, unsigned version) 
    { 
     boost::serialization::split_free(ar, tp, version); 
    } 
} 
} 

例子:

std::chrono::system_clock::time_point installation_point; 
std::chrono::system_clock::time_point trial_end_point; 

/// @brief Obligatory method for any serializable object 
template<class Archive> 
void serialize(Archive & ar, const unsigned int version) { 
    ar & BOOST_SERIALIZATION_NVP(installation_point); 
    ar & BOOST_SERIALIZATION_NVP(trial_end_point); 
} 
相關問題