Boost Fusion adapt declaration for a templated self referential structure

China☆狼群 提交于 2019-12-13 19:12:37

问题


I am trying to declare a self referential tree like structure that is templated on the type of data that the struct can hold. I am running into compilation errors, when trying to declare the boost fusion template adapt definition for this structure. Can anyone help ?

#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;
}

回答1:


@cv_and_he is spot on; 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) )


来源:https://stackoverflow.com/questions/23593733/boost-fusion-adapt-declaration-for-a-templated-self-referential-structure

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!