制作 boost::fusion::result_of::as_set<> “实例不调用它的元素”构造函数

发布于 2024-11-19 15:56:24 字数 3079 浏览 4 评论 0 原文

#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”时,所有节点的构造函数都被成功调用。 谢谢!

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

This code compiles. My question is that when we make instance of "fusion_set_type", all the types' constructors in that fusion set are supposed to get called. However, this "fusion_set_type" doesn't invoke any of its containing types' constructors.
Maybe there is something incorrect in the nested fold/transform operations.
The result of "node_set_type" is "struct node_c, struct node_a, struct node_b", and when I use a mpl::for_each plus a function object to loop through "node_set_type", all node's constructors are successfully getting called.
Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

影子是时光的心 2024-11-26 15:56:24

可能是打字错误?
当我在 ideone 上测试时,构造函数被调用
添加 ::type 如下:

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

Possibly typo?
Constructors were called when I tested on ideone
by adding ::type as the following:

/**********create fusion set instance**************/
fusion_set_type::type instance;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文