如何在boost :: spirit语义操作中将函数对象的结果分配给本地

时间:2012-05-04 04:34:15

标签: c++ boost-spirit boost-spirit-qi

我不确定为什么以下代码在GCC 4.6.3中给出了以下错误

在'boost :: spirit :: _ a = boost :: phoenix :: function :: operator()(const A0&)const中与'operator ='不匹配[与A0 = boost :: phoenix :: actor&gt ;,F = make_line_impl,typename boost :: phoenix :: as_composite,F,A0> :: type = boost :: phoenix :: composite,boost :: fusion :: vector,boost :: spirit :: argument< 0>, boost :: fusion :: void_,boost :: fusion :: void_,boost :: fusion :: void_,boost :: fusion :: void_,boost :: fusion :: void_,boost :: fusion :: void_,boost: :fusion :: void_,boost :: fusion :: void_> >]((*& boost :: spirit :: _ 1))'

甚至可以将延迟函数对象的结果赋给qi占位符吗?

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

using std::string;

using boost::spirit::qi::grammar;
using boost::spirit::qi::rule;
using boost::spirit::qi::space_type;
using boost::spirit::qi::skip_flag;
using boost::spirit::unused_type;

namespace qi = boost::spirit::qi;
namespace phx = boost::phoenix;

struct make_line_impl
{
  int* _context;
  make_line_impl(int* context)
  {
    _context = context;
  }

  template <typename Sig>
  struct result;

  template <typename This, typename Arg>
  struct result<This(Arg const &)>
  {
    typedef int* type;
  };
  template <typename Arg>
  int* operator()(Arg const & content)
  {
    return new int(5);
  }
};


template<typename Iterator>
struct MyGrammar : grammar<Iterator, unused_type, space_type>
{
  rule<Iterator, unused_type, space_type> start;
  rule<Iterator, int*(), space_type> label;
  rule<Iterator, string*(), qi::locals<int*>, space_type> line;

  MyGrammar() : MyGrammar::base_type(start)
  {
    make_line_impl mlei(new int(5));
    phx::function<make_line_impl> make_line(mlei);

    start = *(line);

    line = label[qi::_a = make_line(qi::_1)];
  }
};


int main(int argc, char **argv) {

  string contents;

  qi::phrase_parse(contents.begin(), contents.end(), MyGrammar<string::iterator>(), space_type(), skip_flag::postskip);
    return 0;
}

1 个答案:

答案 0 :(得分:2)

我修复了代码中的一些点以使其编译:

  • 我根据BOOST_RESULT_OF的文档重写了嵌套的::result<>::type逻辑。

    注意如果你在c ++ 11模式下编译,你可能最好定义

     #define BOOST_RESULT_OF_USE_DECLTYPE
    

    在这种情况下,您不必费心使用嵌套的结果类型模板。

  • operator(...)方法需要为const

结果代码:

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

using std::string;

using boost::spirit::qi::grammar;
using boost::spirit::qi::rule;
using boost::spirit::qi::space_type;
using boost::spirit::qi::skip_flag;
using boost::spirit::unused_type;

namespace qi = boost::spirit::qi;
namespace phx = boost::phoenix;

struct make_line_impl
{
  int* _context;
  make_line_impl(int* context)
  {
    _context = context;
  }

  template <typename Arg> struct result { typedef int* type; };

  template <typename Arg>
  int* operator()(Arg const & content) const
  {
    return new int(5);
  }
};


template<typename Iterator>
struct MyGrammar : grammar<Iterator, unused_type, space_type>
{
  rule<Iterator, unused_type, space_type> start;
  rule<Iterator, int*(), space_type> label;
  rule<Iterator, string*(), qi::locals<int*>, space_type> line;

  MyGrammar() : MyGrammar::base_type(start)
  {
    make_line_impl mlei(new int(5));
    phx::function<make_line_impl> make_line(mlei);

    start = *(line);

    line = label[qi::_a = make_line(qi::_1)];
  }
};


int main(int argc, char **argv) {

  string contents;

  qi::phrase_parse(contents.begin(), contents.end(), MyGrammar<string::iterator>(), space_type(), skip_flag::postskip);
    return 0;
}