它是安全的序列化原料的boost ::变种? [英] Is it safe to serialize a raw boost::variant?

查看:143
本文介绍了它是安全的序列化原料的boost ::变种?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

的boost ::变种声称它是值类型。这是否意味着它的安全简单地写出一个boost ::变种的原料再presentation并加载回来后,只要它仅包含POD类型?假定它将被code。通过相同的编译器编译,相同版本的提振重新加载,在相同的架构。

boost::variant claims that it is a value type. Does this mean that it's safe to simply write out the raw representation of a boost::variant and load it back later, as long as it only contains POD types? Assume that it will be reloaded by code compiled by the same compiler, and same version of boost, on the same architecture.

此外,(可能)等价,可以提高::变种可以在共享内存中使用?

Also, (probably) equivalently, can boost::variant be used in shared memory?

推荐答案

对于序列化:它应该工作,是的。但是,你为什么不使用的boost ::变种的探视机制写出包含的变量的实际类型?

Regarding serialisation: It should work, yes. But why don't you use boost::variant's visitation mechanism to write out the actual type contained in the variant?

struct variant_serializer : boost::static_visitor<void> {
    template <typename T>
    typename boost::enable_if< boost::is_pod<T>, void>::type
    operator()( const T & t ) const {
        // ... serialize here, e.g.
        std::cout << t;
    }
};

int main() {

    const boost::variant<int,char,float,double> v( '1' );

    variant_serializer s;
    boost::apply_visitor( s, v );

    return 0;
}

关于共享内存:的boost ::变种不执行堆分配,这样你就可以将其放置到共享内存中就像一个 INT ,假设适当的同步,当然。

Regarding shared memory: boost::variant does not perform heap allocations, so you can place it into shared memory just like an int, assuming proper synchronisation, of course.

不用说,如你所说,上面是唯一有效的,如果变量只能包含POD类型。

Needless to say, as you said, the above is only valid if the variant can only contain POD types.

这篇关于它是安全的序列化原料的boost ::变种?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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