首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >BOOST_FUSION_ADAPT_STRUCT的替代实现

BOOST_FUSION_ADAPT_STRUCT的替代实现
EN

Stack Overflow用户
提问于 2020-07-21 22:17:59
回答 1查看 91关注 0票数 1

来自link的BOOST_FUSION_ADAPT_STRUCT使用情况,即

代码语言:javascript
复制
namespace demo
{
    struct employee
    {
        std::string name;
        int age;
    };
}

BOOST_FUSION_ADAPT_STRUCT(
    demo::employee,
    (std::string, name)
    (int, age))

替换BOOST_FUSION_ADAPT_STRUCT宏的实际实现是什么?

EN

回答 1

Stack Overflow用户

发布于 2020-07-22 01:42:59

我可能依赖于编译器/标志。关于我的:

代码语言:javascript
复制
namespace boost { namespace fusion { namespace traits { template< > struct tag_of<demo::employee > { typedef struct_tag type; }; template< > struct tag_of<demo::employee const> { typedef struct_tag type; }; } namespace extension { template< > struct access::struct_member< demo::employee , 0 > { typedef std::string attribute_type; typedef attribute_type type; template<typename Seq> struct apply { typedef typename add_reference< typename mpl::eval_if< is_const<Seq> , add_const<attribute_type> , mpl::identity<attribute_type> >::type >::type type; constexpr static type call(Seq& seq) { return seq. name; } }; }; template< > struct struct_member_name< demo::employee , 0 > { typedef char const* type; constexpr static type call() { return "name"; } }; template< > struct access::struct_member< demo::employee , 1 > { typedef int attribute_type; typedef attribute_type type; template<typename Seq> struct apply { typedef typename add_reference< typename mpl::eval_if< is_const<Seq> , add_const<attribute_type> , mpl::identity<attribute_type> >::type >::type type; constexpr static type call(Seq& seq) { return seq. age; } }; }; template< > struct struct_member_name< demo::employee , 1 > { typedef char const* type; constexpr static type call() { return "age"; } }; template< > struct struct_size<demo::employee> : mpl::int_<2> {}; template< > struct struct_is_view< demo::employee > : mpl::false_ {}; } } namespace mpl { template<typename> struct sequence_tag; template< > struct sequence_tag<demo::employee> { typedef fusion::fusion_sequence_tag type; }; template< > struct sequence_tag< demo::employee const > { typedef fusion::fusion_sequence_tag type; }; } }

只是为了可读性:

代码语言:javascript
复制
#include <string>
#include <boost/fusion/adapted/struct.hpp>

namespace demo {
    struct employee
    {
        std::string name;
        int age;
    };
}

namespace boost {
    namespace fusion {
        namespace traits {
            template <> struct tag_of<demo::employee> { typedef struct_tag type; };
            template <> struct tag_of<demo::employee const> { typedef struct_tag type; };
        } // namespace traits
        namespace extension {
            template <> struct access::struct_member<demo::employee, 0> {
                typedef std::string attribute_type;
                typedef attribute_type type;
                template <typename Seq> struct apply {
                    typedef typename add_reference<
                        typename mpl::eval_if<is_const<Seq>, add_const<attribute_type>,
                                 mpl::identity<attribute_type>>::type>::type
                                     type;
                    constexpr static type call(Seq& seq) { return seq.name; }
                };
            };
            template <> struct struct_member_name<demo::employee, 0> {
                typedef char const* type;
                constexpr static type call() { return "name"; }
            };
            template <> struct access::struct_member<demo::employee, 1> {
                typedef int attribute_type;
                typedef attribute_type type;
                template <typename Seq> struct apply {
                    typedef typename add_reference<
                        typename mpl::eval_if<is_const<Seq>, add_const<attribute_type>,
                                 mpl::identity<attribute_type>>::type>::type
                                     type;
                    constexpr static type call(Seq& seq) { return seq.age; }
                };
            };
            template <> struct struct_member_name<demo::employee, 1> {
                typedef char const* type;
                constexpr static type call() { return "age"; }
            };
            template <> struct struct_size<demo::employee> : mpl::int_<2> {};
            template <> struct struct_is_view<demo::employee> : mpl::false_ {};
        } // namespace extension
    } // namespace fusion
    namespace mpl {
        template <typename> struct sequence_tag;
        template <> struct sequence_tag<demo::employee> {
            typedef fusion::fusion_sequence_tag type;
        };
        template <> struct sequence_tag<demo::employee const> {
            typedef fusion::fusion_sequence_tag type;
        };
    } // namespace mpl
} // namespace boost

有关实际的替代方案,请参见

用于执行代码生成的

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63016578

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档