std::function 可以序列化吗? [英] Can std::function be serialized?

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

问题描述

这是一个理论问题.假设有一些对象包含订阅这些对象事件的回调函数列表.现在我们想将这些对象存储在磁盘上.std::function 是否可序列化?

This is a theoretical question. Say there are some objects which among others contain lists of callback functions subscribed to events of those objects. Now we want to store those objects on disk. Is a std::function serializable?

推荐答案

没有

每当使用类型擦除(即,将实现细节隐藏在接口后面)时,唯一不知道对象动态类型的可用操作就是接口提供的操作.

Whenever using type erasure (ie, hiding implementation details behind an interface), the only operations available without knowing the dynamic type of the object are those provided by the interface.

C++标准中没有序列化,也没有简单的方法序列化函数(没有反射),因此std::function接口没有提供序列化.

There is no serialization in the C++ Standard, and there is no easy way to serialize functions either (without reflection), thus the std::function interface does not provide serialization.

另一方面,没有什么可以阻止使用提供序列化支持的Callback基类.

On the other hand, nothing prevents you from using a Callback base class that provides serialization support.

这篇关于std::function 可以序列化吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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