我正在尝试创建一个简单的解析器,它使用boost::spirit::x3
. 问题是它x3::char_('#') | x3::char_('.')
似乎有一个 type 的属性boost::variant<char, ?>
。这意味着我必须在boost::get<char>
上使用_attr
,而它应该可以直接转换为char
.
http://ciere.com/cppnow15/x3_docs/spirit/quick_reference/compound_attribute_rules.html,它说A | A -> A
如果使用注释掉的版本,mapChars
那么它可以转换为 a char
,但不能转换为|
.
我在 boost 版本 1.63.0 和 Linux 上。该代码无法在 g++ 和 clang++ 上使用-std=c++14
.
我究竟做错了什么?
#include <iostream>
#include <string>
#include <boost/spirit/home/x3.hpp>
int main() {
std::string s("#");
namespace x3 = boost::spirit::x3;
auto f = [](auto & ctx) {
auto & attr = x3::_attr(ctx);
//char c = attr; // doesn't work
char c = boost::get<char>(attr); // does work
};
auto mapChar = x3::char_('#') | x3::char_('.'); // _attr not convertible to char, is a variant
//auto mapChar = x3::char_('#'); // _attr convertible to char, isn't a variant
auto p = mapChar[f];
auto b = s.begin();
bool success = x3::parse(b, s.end(), p);
std::cout << "Success: " << success << ' ' << (b == s.end()) << '\n';
}