【发布时间】:2014-08-25 21:06:20
【问题描述】:
我认为我在理解我的 boost::spirit::qi 解析器应该如何编写时遇到了问题。我只是想通过语义操作将匹配的子字符串传递给函数。为了大致模仿this boost tutorial,我想出了以下代码:
template<class Iterator>
struct _Calculator : public qi::grammar<Iterator> {
qi::rule<Iterator> number, result; //and a whole bunch of other ones too
qi::rule<Iterator,std::string()> variable;
Code& code;
Variables& variables;
_Calculator(Code& code_, Variables& variables_)
: _Calculator::base_type(result),
code(code_),
variables(variables_)
{
number =
lexeme[(qi::char_("1-9") >> +qi::char_("0-9"))[boost::phoenix::bind(&fPushIntCV, qi::_1, boost::ref(code), boost::ref(variables))]]
| lexeme[("0x" >> +qi::char_("0-9a-fA-F")) [boost::phoenix::bind(&fPushIntCV, qi::_1, boost::ref(code), boost::ref(variables))]]
| lexeme[("0b" >> +qi::char_("0-1")) [boost::phoenix::bind(&fPushIntCV, qi::_1, boost::ref(code), boost::ref(variables))]]
| lexeme[("0" >> +qi::char_("0-7")) [boost::phoenix::bind(&fPushIntCV, qi::_1, boost::ref(code), boost::ref(variables))]]
//some other junk
}
};
typedef _Calculator<std::string::const_iterator> Calculator;
fPushIntCV 的声明如下:
void fPushIntCV (vector<char> my_str, Code& c, Variables& v);
我收到此错误:
function_ptr.hpp:89: error: conversion from 'char' to non-scalar type 'std::vector<char, std::allocator<char> >' requested
当我将fPushIntCV 的声明更改为如下所示:
void fPushIntCV (char my_str, Code& c, Variables& v);
我收到此错误:
function_ptr.hpp:89: error: cannot convert 'std::vector<char, std::allocator<char> >' to 'char' in argument passing
我认为qi::_1 的属性正在发生变化,但是如果我同时包含两个函数原型并且现在传递boost::phoenix::bind 一些模棱两可的重载函数指针,我会得到未解析的引用:
error: no matching function for call to 'bind(<unresolved overloaded function type>, const boost::spirit::_1_type&, ...(我的 ... 用于尾随无关垃圾)
我知道这可能是一个非常简单的错误和一个非常简单的修复,但我有一个很好的时间来理解这个咒语,让增强魔法发挥作用。这个语义动作所期望的函数原型是什么?
【问题讨论】:
标签: c++ boost boost-spirit boost-phoenix