Accessing boost fusion map field name

前端 未结 1 429
广开言路
广开言路 2020-12-16 17:03

I\'ve been trying to use some of the boost fusion stuff to write a regular c struct to file. An XML file seems a good way to capture the data and make it compatible with oth

1条回答
  •  眼角桃花
    2020-12-16 17:40

    #include 
    #include 
    
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    
    namespace fusion=boost::fusion;
    namespace mpl=boost::mpl;
    
    struct  myStructType
    {
        double val1;
        double val2;
        char letter;
        int number;
    }; 
    
    BOOST_FUSION_ADAPT_STRUCT(
        myStructType,
        (double, val1)
        (double, val2)
        (char, letter)
        (int, number)
    )   
    
    
    
    template 
    struct XmlFieldNamePrinter
    {
        XmlFieldNamePrinter(const Sequence& seq):seq_(seq){}
        const Sequence& seq_;
        template 
        void operator() (Index idx) const
        {
            //use `Index::value` instead of `idx` if your compiler fails with it
            std::string field_name = fusion::extension::struct_member_name::call();
    
            std::cout
                << '<' << field_name << '>'
                << fusion::at(seq_)
                << "'
                ;
        }
    };
    template
    void printXml(Sequence const& v)
    {
        typedef mpl::range_c::value > Indices; 
        fusion::for_each(Indices(), XmlFieldNamePrinter(v));
    }
    
    int main()
    {
        myStructType saveMe = { 3.4, 5.6, 'g', 9};
        printXml(saveMe);
    }
    

    0 讨论(0)
提交回复
热议问题