0

I have an stl functional std::function<int(int,int)> fcn_ as a member field of a class. Is there a way to serialize it using boost serialization? If I do the following

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

I got the error

/opt/local/include/boost/serialization/access.hpp:118:9: error: 'class std::function<int(int, int)>' has no member named 'serialize'

Is there a header file (say something like<boost/serialization/vector.hpp>) I can include that implements serialize for std::function? Or is there an easy way to implement one myself?

Thanks!

Ying Xiong
  • 4,578
  • 8
  • 33
  • 69
  • possible duplicate of [Can std::function be serialized?](http://stackoverflow.com/questions/18173339/can-stdfunction-be-serialized) – Ying Xiong Nov 17 '14 at 13:29

3 Answers3

0

I haven't had a look at how Boost serialization works, but here is a 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);
  return 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);
  return in;
}

Please note that this will not work if you are storing lambdas or function objects in your std::functions (as per http://en.cppreference.com/w/cpp/utility/functional/function/target).

A running example can be found coliru.

Tom Knapen
  • 2,277
  • 16
  • 31
  • Thanks for your reply, Tom. But I don't think this would work if I write the serialization to a file and reload it in a different program (or the same program at a different invocation). Probably there is no way to do what I have in mind in C++, as discussed here. See the discussion here [link](http://stackoverflow.com/questions/12338265/serializing-function-objects) and [link](http://stackoverflow.com/questions/18173339/can-stdfunction-be-serialized) – Ying Xiong Nov 04 '14 at 02:14
  • Interesting links, @YingXiong. Sadly these answers were already dupes and clearly show how your question should be considered a duplicate. Of course, you can ask _follow up_ questions ("How would is serialize it since you can't, using the language core features?") – sehe Nov 04 '14 at 07:20
0

You could require my_serializable_function<int(int,int)> which knows how to self-describe and reconstruct from such a descriptor.

In other words: you write the code yourself.

Alternatively, you might look at a scripting engine, which already contains stuff like this (Boost Python, several Lua bindings, v8 engine etc.). Though each comes with their own set of trade-offs and might be overkill.

sehe
  • 374,641
  • 47
  • 450
  • 633
0

Thanks to Tom and sehe's response. After some research, I realized what I had in mind is not really possible in C++ --- it is generally not possible to serialize an std::function object. By "serialize", I mean being able to transfer (read/write) the object as a byte stream from one program to another program, or to the same program but a different invocation on the same or a different machine. The links below has more discussion about this topic:

Serializing function objects

Can std::function be serialized?

Community
  • 1
  • 1
Ying Xiong
  • 4,578
  • 8
  • 33
  • 69