【问题标题】:How do I assign the result of a function object to a local in a boost::spirit semantic action如何在 boost::spirit 语义动作中将函数对象的结果分配给本地
【发布时间】:2012-05-04 04:34:14
【问题描述】:

我不太确定为什么以下代码在 GCC 4.6.3 中会出现以下错误

'boost::spirit::_a = boost::phoenix::function::operator()(const A0&) const [with A0 = boost::phoenix::actor > 中的 'operator=' 不匹配, F = make_line_impl, typename boost::phoenix::as_composite, F, A0>::type = boost::phoenix::composite, boost::fusion::vector, boost::spirit::argument, 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;
}

【问题讨论】:

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


    【解决方案1】:

    我已经修复了代码中的一些问题以使其编译:

    • 我根据BOOST_RESULT_OF的文档重写了嵌套的::result&lt;&gt;::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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-02-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多