c++11 - Is there a way to use Boost serialization on stl functional -
c++11 - Is there a way to use Boost serialization on stl functional -
i have stl functional std::function<int(int,int)> fcn_
fellow member field of class. there way serialize using boost serialization? if following
template<class archive> void serialize(archive &ar, const unsigned int version) { ar & fcn_; }
i got error
/opt/local/include/boost/serialization/access.hpp:118:9: error: 'class std::function<int(int, int)>' has no fellow member named 'serialize'
is there header file (say like<boost/serialization/vector.hpp>
) can include implements serialize
std::function
? or there easy way implement 1 myself?
thanks!
i haven't had @ how boost serialization works, here possible approach.
template<typename t> std::ostream& serialize(std::ostream& out, const std::function<t>& fn) { using decay_t = typename std::decay<t>::type; if(fn.target_type() != typeid(decay_t)) throw std::runtime_error(std::string(typeid(decay_t).name()) + " != " + fn.target_type().name()); const auto ptr = fn.template target<decay_t>(); out << reinterpret_cast<void*>(*ptr); homecoming out; } template<typename t> std::istream& deserialize(std::istream& in, std::function<t>& fn) { using decay_t = typename std::decay<t>::type; void* ptr = nullptr; in >> ptr; fn = reinterpret_cast<decay_t>(ptr); homecoming in; }
please note not work if storing lambdas or function objects in std::function
s (as per http://en.cppreference.com/w/cpp/utility/functional/function/target).
a running illustration can found coliru.
c++11 serialization boost stl boost-serialization
Comments
Post a Comment