Spirit unable to assign attribute to single-element struct (or fusion sequence)

前端 未结 1 1233
我在风中等你
我在风中等你 2020-12-07 02:38

My goal is to have my qi::grammar return an attribute. I\'m having significant difficulty doing this with a spirit::lexer though.

I\'d expe

相关标签:
1条回答
  • 2020-12-07 02:53

    I ended up realizing that the struct I defined is being used in spirit as a tuple. Because spirit will try to minimize the groups (like, an optional<int, int> is an optional<int>). Therefore, I guessed that a tuple<A> will be converted to an A. Which seems to be the case.

    I was able to further reduce the test case of broken code to the following:

    #include <boost/fusion/include/adapt_struct.hpp>
    #include <boost/spirit/home/qi.hpp>
    #include <string>
    
    struct ident {
        std::string a;
    };
    
    BOOST_FUSION_ADAPT_STRUCT(ident,
            (std::string, a)
            )
    
    int main() {
        boost::spirit::qi::rule<const char*, ident()> r;
        r = boost::spirit::lexeme["abc"];
    }
    

    From the following mailing list postings (1, 2) that I found, I can work around this issue by doing:

    r = boost::spirit::lexeme["abc"] >> boost::spirit::eps;
    

    While not really elegant, it solves the problem at least. If someone else has a method to do the single element struct, I'd be greatly interested though.

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