Search code examples
c++compiler-errorsboost-spirit-qi

boost::spirit -- compiler error trying to compile most simple code


Somebody mentioned, that there is already an answer to this question. Well, the other person was looking for a parse error regarding boost::spirit. Since boost::spirit is a parser-generator, one might think that he wants to know, how to generate a good parse error. I'm looking to solve a compiler error.

When I attempt to compile the code below, I'm always getting a compiler error, that the std::pair cannot be constructed from a single int. WTH?

#include <boost/spirit/include/qi.hpp>


namespace qi = boost::spirit::qi;
namespace spirit = boost::spirit;
namespace ascii = boost::spirit::ascii;

template<typename Iterator>
struct pair:qi::grammar<Iterator, std::pair<int, int>(), ascii::space_type>
{   
    qi::rule<Iterator, std::pair<int, int>(), ascii::space_type> m_sNameValue;
    pair(void)
        :pair::base_type(m_sNameValue)
    {   m_sNameValue %= spirit::int_ >> spirit::int_;
    }
};

int main(int, char**)
{
    static const char s_ap[] = "3 4";
    pair<const char*> sGrammar;
    const char *pIter = s_ap;
    const char *const pEnd = s_ap + sizeof s_ap;
    std::pair<int, int> sValues;
    if (phrase_parse(pIter, pEnd, sGrammar, boost::spirit::ascii::space, sValues) && pIter == pEnd)
        std::cerr << "parsing successful!" << std::endl;
    else
        std::cerr << "parsing failed!" << std::endl;
}

Solution

  • Try including:

    #include <boost/fusion/include/std_pair.hpp>