boost::spirit::x3 中的简单字符串解析器无法正常工作

Simple string parser in boost::spirit::x3 not working

出于学习目的,我正在尝试编写一个简单的解析器,它接受字符串文字并使用来自 boost 的 x3 库将其放入自定义结构中。但是,以下改编自示例 here 的最小示例无法编译。

#include <iostream>
#include <boost/spirit/home/x3.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
#include <string>

namespace x3 = boost::spirit::x3;

namespace ast {
struct Symbol {
    std::string val;
  };
}
BOOST_FUSION_ADAPT_STRUCT(
ast::Symbol,
val
)

namespace parser {
  x3::rule<class Symbol, ast::Symbol> const
  symbol = "symbol";

  auto const symbol_def = x3::string("asd");// | x3::string("qwe");

  BOOST_SPIRIT_DEFINE(
  symbol
  );
}

int main(int argc, char** argv) {
  std::string input = "asd";
  if (argc > 1) {
    input = argv[1];
  }
  std::string::const_iterator begin = input.begin();
  std::string::const_iterator end = input.end();

  ast::Symbol sym;
  bool success = x3::phrase_parse(begin, end, parser::symbol, 
                                  x3::ascii::space, sym);
  std::cout << success << std::endl;
  std::cout << sym.val << std::endl;
  return 0;
 }

这给出了一个很长的模板编译器错误,归结为

cannot convert ‘dest’ (type ‘ast::Symbol’) to type 
‘boost::spirit::x3::traits::variant_attribute’

这对我来说没有意义,因为解析器 x3::string 应该有一个字符串属性,而 ast::Symbol 结构有一个字符串字段,x3 应该能够自动填充,因为我已经调整了使用 Fusion 宏进行结构化。更令人困惑的是,如果我将解析器的定义更改为 read

auto const symbol_def = x3::string("asd") | x3::string("qwe");

它编译并工作,即使现在解析器应该有一个变量类型的属性。也许有人可以澄清为什么会发生这种情况,因为我似乎遗漏了一些有关图书馆工作方式的信息。

我认为这是一个已解决的问题: