【发布时间】:2017-02-10 07:31:43
【问题描述】:
我正在尝试创建一个简单的解析器,它使用boost::spirit::x3 获取两个可能的字符之一。问题是x3::char_('#') | x3::char_('.') 似乎有一个boost::variant<char, ?> 类型的属性。这意味着我必须在_attr 上使用boost::get<char>,而它应该可以直接转换为char。
http://ciere.com/cppnow15/x3_docs/spirit/quick_reference/compound_attribute_rules.html,上面写着A | A -> A
如果使用注释掉的mapChars 版本,则它可以转换为char,但不能转换为|。
我使用的是 boost 版本 1.63.0 和 Linux。代码无法在带有-std=c++14 的 g++ 和 clang++ 上编译。
我做错了什么?
#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';
}
【问题讨论】:
-
我认为
x3::char_("#.")应该做你想做的事。当然这并不能解决A|A->variant<A>的问题。 -
对于这个示例,这是非常等价的
标签: c++ boost boost-spirit boost-spirit-x3