Boost Spirit v2 gcc compilation error which does not show up using msvc
I recently wrote some spirit parsing code in windows, which I recently have tried to get build on an ubuntu box and have ran into a compilation error that I am struggling with.
After some hacking and slashing, I have managed to come up with this example code snippet which exhibits the same behavior:
struct FooParser
: spirit::qi::grammar<
std::string::const_iterator,
double(),
spirit::qi::ascii::space_type>
{
FooParser() : FooParser::base_type(a_rule)
{
using namespace boost::spirit::qi;
a_rule = double_;
}
spirit::qi::rule<
string::const_iterator,
double(),
spirit::qi::ascii::space_type> a_rule;
};
which is then passed to a phrase_parse like so:
double result;
std::string txt;
FooParser foobar;
//...
if(phrase_parse(txt.begin(), txt.end(), foobar, space, result))
{
//do something
}
And when compiled, generates the following error:
boost/spirit/home/qi/reference.hpp:41: error: no matching function for call to
‘boost::spirit::qi::rule<__gnu_cxx::__normal_iterator<const char*, std::basic_string<char,
std::char_traits<char>, std::allocator<char> > >, double(),
boost::proto::exprns_::expr<boost::proto::tag::terminal,
boost::proto::argsns_::term<boost::spirit::tag::char_code<boost::spirit::tag::space,
boost::spirit::char_encoding::ascii> >, 0l>, boost::fusion::unused_type,
boost::fusion::unused_type>::parse(__gnu_cxx::__normal_iterator<char*,
std::basic_string<char, std::char_traits<char>, std::allocator<char> > >&, const
__gnu_cxx::__normal_iterator<char*, std::basic_string<char, st开发者_如何学运维d::char_traits<char>,
std::allocator<char> > >&, const boost::fusion::unused_type&, const
boost::spirit::qi::char_class<boost::spirit::tag::char_code<boost::spirit::tag::space,
boost::spirit::char_encoding::ascii> >&, double&) const’
The thing that I find frustrating is that visual studio seems more than happy to compile and run the code. I am hoping that the wise internets can show me where I have erred.
I think the problem here is the begin()
and end()
of std::string
, try this:
std::string::const_iterator begin = txt.begin();
std::string::const_iterator end = txt.end();
then pass that in:
phrase_parse(begin, end, foobar, space, result)
The problem I believe is that everywhere else you are using the type const_iterator
, but begin()
, end()
on a non const string returns a normal iterator.
The key part of the error is this bit:
parse(__gnu_cxx::__normal_iterator, std::allocator > >&, const __gnu_cxx::__normal_iterator, std::allocator > >&
精彩评论