Boost fusion sequence type and name identification for structs and class

前端 未结 2 816
無奈伤痛
無奈伤痛 2021-01-05 23:14

I am trying to use boost fusion for one of my projects and I an figuring out how to get type names and variable names for structures and classes.

#include &         


        
2条回答
  •  太阳男子
    2021-01-05 23:49

    I distilled the answer by sehe into something much simpler, provided you are using C++14

    #include 
    
    #include 
    #include 
    #include 
    #include 
    
    struct MyStruct {
        std::string foo;
        double bar;
    };
    
    BOOST_FUSION_ADAPT_STRUCT(MyStruct,
                              foo,
                              bar)
    
    namespace fuz = boost::fusion;
    namespace mpl = boost::mpl;
    
    int main(int argc, char* argv[]) {
      MyStruct dummy{"yo",3.14};
    
      fuz::for_each(mpl::range_c<
                    unsigned, 0, fuz::result_of::size::value>(),
                    [&](auto index){
          std::cout << "Name: "
                    << fuz::extension::struct_member_name::call()
                    << " Value: "
                    << fuz::at_c(dummy) << std::endl; 
        });
    
    }
    

    Outputs:

    Name: foo Value: yo
    Name: bar Value: 3.14
    

    See it live on coliru

提交回复
热议问题