【问题标题】:Alternative implementation for BOOST_FUSION_ADAPT_STRUCTBOOST_FUSION_ADAPT_STRUCT 的替代实现
【发布时间】:2020-07-21 14:17:58
【问题描述】:

link,BOOST_FUSION_ADAPT_STRUCT的用法是

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

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

替换使用 BOOST_FUSION_ADAPT_STRUCT 宏的实际实现是什么。

【问题讨论】:

    标签: c++ boost boost-fusion


    【解决方案1】:

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

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

    只是为了可读性:

    #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
    

    有关实际替代方案,请参阅

    【讨论】:

      猜你喜欢
      • 2013-10-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-24
      • 2013-12-04
      • 2011-02-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多