简体   繁体   English

BOOST_FUSION_ADAPT_STRUCT 的替代实现

[英]Alternative implementation for BOOST_FUSION_ADAPT_STRUCT

From link , the usage of BOOST_FUSION_ADAPT_STRUCT usage which is链接, BOOST_FUSION_ADAPT_STRUCT 用法的用法是

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

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

What will be the actual implementation to replace the use of the BOOST_FUSION_ADAPT_STRUCT macro.替换使用 BOOST_FUSION_ADAPT_STRUCT 宏的实际实现是什么。

I might depend on compiler/flags.我可能依赖于编译器/标志。 On mine:在我的:

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

Just for readability:只是为了可读性:

#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

For actual alternatives, see有关实际替代方案,请参阅

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 BOOST_FUSION_ADAPT_STRUCT的限制 - Limits of BOOST_FUSION_ADAPT_STRUCT BOOST_FUSION_ADAPT_STRUCT的boost :: spirit编译问题 - boost::spirit compile issue with BOOST_FUSION_ADAPT_STRUCT 我可以将BOOST_FUSION_ADAPT_STRUCT与继承的东西一起使用吗? - Can I use BOOST_FUSION_ADAPT_STRUCT with inherited stuff? 如何将BOOST_FUSION_ADAPT_STRUCT与子结构一起使用? - How to use BOOST_FUSION_ADAPT_STRUCT with substructures? 当BOOST_FUSION_ADAPT_STRUCT的参数数量为2时,Boost Fusion编译失败 - Boost Fusion compile failure when number of arguments to BOOST_FUSION_ADAPT_STRUCT is 2 使用fusion :: at时C ++:BOOST_FUSION_ADAPT_STRUCT错误 - C++: BOOST_FUSION_ADAPT_STRUCT Error When Using fusion::at 将 boost::qi::rule 与 BOOST_FUSION_ADAPT_STRUCT 一起使用的正确方法是什么? - What is the correct way to use boost::qi::rule with BOOST_FUSION_ADAPT_STRUCT? 我可以将 BOOST_FUSION_ADAPT_STRUCT 与由 std::vector 组成的结构一起使用吗? - Can I use BOOST_FUSION_ADAPT_STRUCT with struct consisting of std::vector? boost :: spirit递归命令式C ++语法:BOOST_FUSION_ADAPT_STRUCT失败 - boost::spirit recursive imperative c++ grammar: BOOST_FUSION_ADAPT_STRUCT fails 与超过64个成员的BOOST_FUSION_ADAPT_STRUCT有什么关系? - What's the deal with BOOST_FUSION_ADAPT_STRUCT with more than 64 members?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM