2

I have this typical Spirit code.

#include <boost/spirit/home/x3.hpp>
#include <boost/fusion/container/vector.hpp>
#include<iostream>
using std::cout;

int main(){

    namespace x3 = boost::spirit::x3;
    boost::fusion::vector<double, double> p;
    x3::phrase_parse(
        s.begin(), s.end(), 
        x3::double_ >> x3::double_, x3::space, 
        p
    );
    assert( boost::fusion::at_c<0>(p) == 1.2 );
    assert( boost::fusion::at_c<1>(p) == 3.4 );
}

I am wondering is it possible to deduce the type of the expected attribute from the grammar?

Something like

using att_type = decltype(x3::double_ >> x3::double_)::result_type;

that deduces boost::fusion::vector<double, double>.

I found this http://boost-spirit.com/home/2010/01/31/what-is-the-attribute-type-exposed-by-a-parser/ but it doesn't work with x3 or I am not including the right headers.

alfC
  • 14,261
  • 4
  • 67
  • 118
  • @jv_ , what happened with the answer? I was about to try it. it didn't compile? – alfC Sep 20 '16 at 19:48
  • 2
    `using att_type = x3::traits::attribute_of> x3::double_),x3::unused_type>::type;` seems to work, but I'm not confident in that answer. I think not having the tags [tag:boost] and/or [tag:boost-spirit] may limit the visibility of your answer, and make it difficult to reach someone who might be able to help. – llonesmiz Sep 21 '16 at 02:16
  • @jv_ , Your code actually works for me, an unexpected detail is that it the deduced attribute is a `boost::fusion:deque` (instead of a `fusion::vector`). There must be a good reason for that, and `boost::fusion::as_vector` is always available. – alfC Sep 21 '16 at 06:29

0 Answers0