2011-07-13 24 views
1
#include <iostream> 

#include <boost/mpl/front.hpp> 
#include <boost/mpl/pop_front.hpp> 
#include <boost/mpl/push_front.hpp> 
#include <boost/mpl/push_back.hpp> 
#include <boost/mpl/pop_back.hpp> 
#include <boost/mpl/fold.hpp> 
#include <boost/mpl/transform.hpp> 
#include <boost/mpl/vector.hpp> 
#include <boost/mpl/set.hpp> 
#include <boost/mpl/back_inserter.hpp> 
#include <boost/fusion/include/set.hpp> 
#include <boost/fusion/include/mpl.hpp> 

/**********definition of nodes**************/ 
struct node_base 
{}; 
struct node_a : public node_base 
{ 
node_a(){std::cout << "node_a::Ctor"<< std::endl;} 
}; 
struct node_b : public node_base 
{ 
node_b(){std::cout << "node_b::Ctor"<< std::endl;} 
}; 
struct node_c : public node_base 
{ 
node_c(){std::cout << "node_c::Ctor"<< std::endl;} 
}; 

struct empty_node {}; 

/***********definition of table*************/ 
struct my_table : ::boost::mpl::vector3< 
::boost::mpl::vector3<node_a, node_b, node_c> 
,::boost::mpl::vector3<node_b, node_c, node_a> 
,::boost::mpl::vector3<node_c, node_a, node_b> 
> {}; 

/*************meta-functions**************/ 
struct make_tag_vector_ 
{ 
template<class NODE1, class NODE2> 
struct apply 
{ 
    typedef typename ::boost::mpl::vector<NODE1>::type type; 
}; 
}; 

struct fold_table 
{ 
template<class LIST, class VECTOR> 
struct apply 
{ 
    typedef typename ::boost::mpl::front<VECTOR>::type parent; 
    typedef typename ::boost::mpl::pop_front<VECTOR>::type children; 
    typedef typename ::boost::mpl::pop_back<children>::type tmp; 
    typedef typename ::boost::mpl::push_front<tmp, empty_node>::type right_shift_children; 

    typedef typename ::boost::mpl::transform< 
     children 
     , right_shift_children 
     , make_tag_vector_ 
     , ::boost::mpl::back_inserter<LIST> 
    >::type type; 
}; 
}; 

template<class TABLE> 
struct create_table 
{ 
typedef typename ::boost::mpl::fold< 
    TABLE 
    , ::boost::mpl::vector0<> 
    , fold_table 
>::type type; 
}; 

/**********process table**************/ 
typedef create_table<my_table>::type table_type; 

typedef ::boost::mpl::reverse_fold< 
table_type 
, ::boost::mpl::set0<> 
, ::boost::mpl::insert< 
    ::boost::mpl::placeholders::_1 
    , ::boost::mpl::front<::boost::mpl::placeholders::_2> 
> 
>::type node_set_type; 

/**********result of node_set_type is :**************/ 
// struct node_c 
// struct node_a 
// struct node_b 

/**********convert to fusion set type**************/ 
typedef ::boost::fusion::result_of::as_set<node_set_type> fusion_set_type; 

/**********create fusion set instance**************/ 
fusion_set_type instance; 

此代碼編譯。我的問題是,當我們創建「fusion_set_type」的實例時,該融合集合中所有類型的構造函數都應該被調用。但是,這個「fusion_set_type」不會調用它的任何包含類型的構造函數。 也許在嵌套的摺疊/轉換操作中有些不正確。 「node_set_type」的結果是「struct node_c,struct node_a,struct node_b」,並且當我使用mpl :: for_each加上一個函數對象來遍歷「node_set_type」時,所有節點的構造函數都成功調用。 謝謝!使boost :: fusion :: result_of :: as_set <>的實例不調用其元素的構造函數

回答

1

可能是錯字?當我在ideone 測試加入::type爲以下 構造被稱爲:

/**********create fusion set instance**************/ 
fusion_set_type::type instance; 
+0

呀,這個工程。非常感謝你! 我認爲我最初的問題是融合集合中元素的數量超過了它的最大數量是10.FUSION_MAX_SET_SIZE需要明確定義。 – Multithrdi

+0

不客氣:-) –

相關問題