简体   繁体   English

使用 boost fusion 获取结构名称

[英]getting the structure name with boost fusion

Using boost::fusion is possible to iterate an adapted structure and get the name of each member of that structure.使用boost::fusion可以迭代一个适应的结构并获取该结构的每个成员的名称。

Is there a way to retrieve also the name of the structure some way?有没有办法以某种方式检索结构的名称?

What I woulld like to do is the following: given the current scenario我想做的是以下几点:鉴于当前情况

namespace inner { 
struct test_struct {
  int a;
  int b;
} }

BOOST_FUSION_ADAPT_STRUCT( inner::test_struct, a, b );      

I'd like to have a function that will return "test_struct" (or "inner::test_struct" )我想要一个 function 将返回 "test_struct" (或 "inner::test_struct" )

I did check the header file[*] containing struct_size and other extension classes but I haven't found any with that purpose.我确实检查了包含struct_size和其他扩展类的 header 文件[*],但我没有找到任何用于此目的的文件。

Do you know if there is anything out there to do that?你知道那里有什么可以做的吗?

[*] boost/fusion/adapted/struct/detail/extension.hpp [*] boost/fusion/adapted/struct/detail/extension.hpp

That's not a feature.这不是一个功能。 You can see it by inspecting the output of the preprocessor.您可以通过检查预处理器的 output 看到它。

You can see the struct_member_name extension, but no such literals for the struct name:您可以看到struct_member_name扩展名,但结构名称没有这样的文字:

namespace boost {
    namespace fusion {
        namespace traits {
            template <> struct tag_of<inner::test_struct> { typedef struct_tag type; };
            template <> struct tag_of<inner::test_struct const> {
                typedef struct_tag type;
            };
        } // namespace traits
        namespace extension {
            template <> struct access::struct_member<inner::test_struct, 0> {
                struct deduced_attr_type {
                    static const inner::test_struct& obj;
                    typedef boost::type_of::remove_cv_ref_t<decltype(obj.a)> type;
                };
                typedef deduced_attr_type::type 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.a; }
                };
            };
            template <> struct struct_member_name<inner::test_struct, 0> {
                typedef char const* type;
                constexpr static type call() { return "a"; }
            };
            template <> struct access::struct_member<inner::test_struct, 1> {
                struct deduced_attr_type {
                    static const inner::test_struct& obj;
                    typedef boost::type_of::remove_cv_ref_t<decltype(obj.b)> type;
                };
                typedef deduced_attr_type::type 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.b; }
                };
            };
            template <> struct struct_member_name<inner::test_struct, 1> {
                typedef char const* type;
                constexpr static type call() { return "b"; }
            };
            template <> struct struct_size<inner::test_struct> : mpl::int_<2> {};
            template <> struct struct_is_view<inner::test_struct> : mpl::false_ {};
        } // namespace extension
    } // namespace fusion
    namespace mpl {
        template <typename> struct sequence_tag;
        template <> struct sequence_tag<inner::test_struct> {
            typedef fusion::fusion_sequence_tag type;
        };
        template <> struct sequence_tag<inner::test_struct const> {
            typedef fusion::fusion_sequence_tag type;
        };
    } // namespace mpl
} // namespace boost

As always, it may not be too hard to add your own macro to get the extra information.与往常一样,添加自己的宏来获取额外信息可能并不难。 See eg Parsing Selector struct with alternating tokens using Boost Spirit X3 or Boost fusion sequence type and name identification for structs and class请参阅使用 Boost Spirit X3Boost 融合序列类型和名称标识的结构和 class的交替标记解析选择器结构

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM