boost ::变体 声称这是一种值类型。这是否意味着只要仅包含POD类型,就可以简单地写出Boost ::变体的原始表示并在以后加载是安全的?假设它将通过同一架构上的同一编译器和相同版本的Boost编制的代码重新加载。

另外,(可能)等效地,可以在共享内存中使用boost ::变体吗?

有帮助吗?

解决方案

关于序列化:它应该起作用,是的。但是你为什么不使用 boost::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::variant 不执行堆分配,因此您可以将其放入共享内存中 int, 当然,假设正确同步。

不用说,正如您所说,上述仅在变体只能包含POD类型的情况下才有效。

其他提示

尝试包括bubost/serialization/variant.hpp;它为您完成工作。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top