序列化函数对象 [英] Serializing function objects

查看:171
本文介绍了序列化函数对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以序列化和反序列化 std :: function ,一个函数对象或C ++中的一般闭包?怎么样? C ++ 11是否方便了?

Is it possible to serialize and deserialize a std::function, a function object, or a closure in general in C++? How? Does C++11 facilitate this? Is there any library support available for such a task (e.g., in Boost)?

例如,假设一个C ++程序有一个 std ::函数,它需要被传递(例如通过TCP / IP套接字)到驻留在另一台机器上的另一个C ++程序。

For example, suppose a C++ program has a std::function which is needed to be communicated (say via a TCP/IP socket) to another C++ program residing on another machine. What do you suggest in such a scenario?



编辑

为了说明,要移动的函数应该是纯的,并且没有副作用。所以我没有安全或状态不匹配的问题。

To clarify, the functions which are to be moved are supposed to be pure and side-effect-free. So I do not have security or state-mismatch problems.

问题的解决方案是构建一个小的嵌入式领域特定的语言,并序列化其抽象语法树。
我希望我可以找到一些语言/库支持移动一个机器无关的函数表示。

A solution to the problem is to build a small embedded domain specific language and serialize its abstract syntax tree. I was hoping that I could find some language/library support for moving a machine-independent representation of functions instead.

推荐答案

p>否。

C ++没有内置的序列化支持,从来没有想过将代码从一个进程传输到另一个进程,从而避免一个机器到另一个进程。可能这样做的语言通常具有IR(与机器无关的代码的中间表示)和反射。

C++ has no built-in support for serialization and was never conceived with the idea of transmitting code from one process to another, lest one machine to another. Languages that may do so generally feature both an IR (intermediate representation of the code that is machine independent) and reflection.

因此,你留下自己写一个用于传输的协议您需要的操作和DSL方法肯定是可行的...取决于您希望执行的任务的多样性和性能的需要。

So you are left with writing yourself a protocol for transmitting the actions you want, and the DSL approach is certainly workable... depending on the variety of tasks you wish to perform and the need for performance.

另一种解决方案是去与现有的语言。例如,Redis NoSQL数据库嵌入了一个LUA引擎,并且可以执行LUA脚本,您可以这样做并在网络上传输LUA脚本。

Another solution would be to go with an existing language. For example the Redis NoSQL database embeds a LUA engine and may execute LUA scripts, you could do the same and transmit LUA scripts on the network.

这篇关于序列化函数对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆