【问题标题】:Boost Fusion adapt declaration for a templated self referential structureBoost Fusion 为模板化自引用结构调整声明
【发布时间】:2014-05-11 14:21:45
【问题描述】:

我正在尝试声明一个类似结构的自引用树,该结构以该结构可以保存的数据类型为模板。在尝试为此结构声明 boost fusion 模板适应定义时,我遇到了编译错误。有人可以帮忙吗?

#include <iostream>
#include <string>
#include <memory>
#include <vector>

#include <boost/lexical_cast.hpp>
#include <boost/dynamic_bitset.hpp>

#include <boost/fusion/adapted/struct/adapt_struct.hpp>
#include <boost/fusion/include/adapt_struct.hpp>

struct A
{
    int a_val;
    A() : a_val(0) {}
    A(int a) : a_val(a) {}

    friend std::ostream& operator<<(std::ostream &output, A &a_obj)
    {
        output << a_obj.a_val;
        return output;
    }
};

template <typename tree_type_t>
struct simple_tree_t
{
    simple_tree_t() {}
    simple_tree_t( tree_type_t t_obj):tree_data(t_obj) {}
    tree_type_t tree_data;

    std::vector < std::shared_ptr < tree_type_t > > sub_tree;

    typedef typename std::vector < std::shared_ptr < tree_type_t > >::iterator sub_tree_iterator;

    sub_tree_iterator sub_tree_begin() { return sub_tree.begin(); }
    sub_tree_iterator sub_tree_end() { return sub_tree.end(); }

    friend std::ostream& operator<<(std::ostream &output, simple_tree_t &tree)
    {
        output << tree.tree_data;
        std::for_each ( tree.sub_tree_begin(), 
                        tree.sub_tree_end(), 
                        ([&](const std::shared_ptr < A > f){
                            output << *f;
                            }) );
        return output;
    }
};

BOOST_FUSION_ADAPT_STRUCT ( A,
                                (int , a_val) )

BOOST_FUSION_ADAPT_TPL_STRUCT ( (tree_type_t)
                                (simple_tree_t) (tree_type_t)
                                (tree_type_t, tree_data)
                                (typename std::vector < std::shared_ptr < tree_type_t > > , sub_tree) )

int main(void)
{
    simple_tree_t<A> t_i, t_o;

    t_i.tree_data = A(10);
    t_i.sub_tree.push_back(std::make_shared<A>(A(20)));
    t_i.sub_tree.push_back(std::make_shared<A>(A(30)));

    std::cout << t_i << std::endl;

    return 0;
}

【问题讨论】:

标签: c++ boost boost-fusion


【解决方案1】:

@cv_and_he 在现场; Live On Coliru

BOOST_FUSION_ADAPT_TPL_STRUCT ( (tree_type_t),
                                (simple_tree_t) (tree_type_t),
                                (tree_type_t, tree_data)
                                (typename std::vector < std::shared_ptr < tree_type_t > > , sub_tree) )

【讨论】:

  • @balasbellobas 如果这个答案对你有帮助,你应该accept it
猜你喜欢
  • 1970-01-01
  • 2016-06-09
  • 2011-08-01
  • 2011-03-18
  • 2014-06-11
  • 2010-11-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多