簡體   English   中英

使用 boost fusion 獲取結構名稱

[英]getting the structure name with boost fusion

使用boost::fusion可以迭代一個適應的結構並獲取該結構的每個成員的名稱。

有沒有辦法以某種方式檢索結構的名稱?

我想做的是以下幾點:鑒於當前情況

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

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

我想要一個 function 將返回 "test_struct" (或 "inner::test_struct" )

我確實檢查了包含struct_size和其他擴展類的 header 文件[*],但我沒有找到任何用於此目的的文件。

你知道那里有什么可以做的嗎?

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

這不是一個功能。 您可以通過檢查預處理器的 output 看到它。

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

與往常一樣,添加自己的宏來獲取額外信息可能並不難。 請參閱使用 Boost Spirit X3Boost 融合序列類型和名稱標識的結構和 class的交替標記解析選擇器結構

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM