C++ 使用Boost_fusion_ADAPT_ADT对类进行Boost fusion序列化

C++ 使用Boost_fusion_ADAPT_ADT对类进行Boost fusion序列化,c++,boost,boost-fusion,C++,Boost,Boost Fusion,我正在尝试使用boost fusion为类获取序列化模块。我已经将我的类转换为boost::fusion序列。下面这个例子是迈克尔·凯斯在boostcon 13的演讲幻灯片 Michael解释的示例对struct类型很有效。这一点无法应用于类类型。我错过了什么 #include <iostream> #include <typeinfo> #include <string> #include <boost/fusion/include/sequence

我正在尝试使用boost fusion为类获取序列化模块。我已经将我的类转换为boost::fusion序列。下面这个例子是迈克尔·凯斯在boostcon 13的演讲幻灯片

Michael解释的示例对struct类型很有效。这一点无法应用于类类型。我错过了什么

#include <iostream>
#include <typeinfo>
#include <string>
#include <boost/fusion/include/sequence.hpp>
#include <boost/fusion/include/algorithm.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/fusion/include/adapt_adt.hpp>
#include <boost/fusion/include/is_sequence.hpp>
#include <boost/mpl/eval_if.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/type_traits.hpp> 

#include <vector>
#include <list>

using namespace std;

template< typename T >
void serialize(T v)
{
boost::fusion::for_each( v, (serial_out()) );
}

struct serial_out
{
template< typename T > 
void operator() ( T & v , typename std::enable_if<!boost::fusion::traits::is_sequence<T>::value>::type* = 0 ) const
{ 
    simple::serialize<T>::write(v); 
}

template< typename T > 
void operator() ( T & v , typename std::enable_if<boost::fusion::traits::is_sequence<T>::value>::type* = 0  ) const
{ 
    serialize(v); 
}

template< typename T >
void operator()( std::vector<T> & v ) const
{
    simple::serialize<int>::write(v.size());
    std::for_each(v.begin(),v.end(),*this);
}

template< typename T >
void operator()( std::list<T> & v ) const
{
    simple::serialize<int>::write(v.size());
    std::for_each(v.begin(),v.end(),*this);
}
};

namespace simple
{
template<typename T> struct serialize{};
template<> struct serialize<int>
{
    static void write(int v) { cout << v << endl; }
};
template<> struct serialize<float>
{
    static void write(float v) { cout << v << endl;  }
};
template<> struct serialize<std::string>
{
    static void write(std::string v)
    {
        cout << v << endl; 
    }
};
}


class secret_point
{
public:
secret_point(double x, double y) : x_(x), y_(y) {}
double get_x() const { return x_; }
void set_x(double d) { x_=d; }
double get_y() const { return y_; }
void set_y(double d) { y_=d; }
private:
double x_, y_;
};

BOOST_FUSION_ADAPT_ADT( secret_point, (double, double, obj.get_x(), obj.set_x(val) ) (double, double, obj.get_y(), obj.set_y(val) ) )

int main(int argc, char *argv[]) 
{
secret_point p(112,233);
serialize(p);

return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
模板
无效序列化(TV)
{
boost::fusion::for_each(v,(serial_out());
}
结构串行输出
{
模板
void运算符()(T&v,typename std::enable_if::value>::type*=0)常量
{ 
simple::serialize::write(v);
}
模板
void运算符()(T&v,typename std::enable_if::type*=0)常量
{ 
连载(五);
}
模板
void运算符()(std::vector&v)常量
{
simple::serialize::write(v.size());
std::for_each(v.begin()、v.end()、*this);
}
模板
void运算符()(std::list&v)常量
{
simple::serialize::write(v.size());
std::for_each(v.begin()、v.end()、*this);
}
};
名称空间简单
{
模板结构序列化{};
模板结构序列化
{

静态void write(intv){我不知道这是否正确(我使用的是反复试验),但似乎解决了您眼前的问题。我最初在
simple
中专门化
adt\u属性\u代理的方法似乎是错误的(这真的很不简单)。我认为(只更改
serial\u out
而不修改
simple::serialize
)更有意义(但我也不认为它是正确的)。我同意,我试图以类似的方式更改程序。这就是我得到的。感谢链接,我不知道文档的那一部分。您假设列表(或向量)中的任何内容都是POD。使用
简单序列化器
更改
序列化POD
似乎可以解决该问题(您的负载可能也会有同样的问题,但我没有时间测试它)。