【问题标题】:getting the structure name with boost fusion使用 boost fusion 获取结构名称
【发布时间】:2020-08-26 02:38:51
【问题描述】:

使用boost::fusion 可以迭代适应的结构并获取该结构的每个成员的名称。

有没有办法以某种方式检索结构的名称?

我想做的是:在当前情况下

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

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

我想要一个返回“test_struct”(或“inner::test_struct”)的函数

我确实检查了包含struct_size 和其他扩展类的头文件[*],但我没有找到任何用于此目的的文件。

你知道有什么可以做的吗?

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

【问题讨论】:

    标签: c++ templates boost boost-fusion


    【解决方案1】:

    这不是一个功能。您可以通过检查预处理器的输出来查看它。

    您可以看到 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
    

    与往常一样,添加您自己的宏来获取额外信息可能并不难。参见例如Parsing Selector struct with alternating tokens using Boost Spirit X3Boost fusion sequence type and name identification for structs and class

    【讨论】:

    • 嗨,谢赫。感谢您向我指出代码的内部。我会按照你的建议包装它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-11
    • 1970-01-01
    • 2018-08-29
    相关资源
    最近更新 更多