gpt4 book ai didi

c++ - boost spirit x3 (A | AN) 属性类型是 variant 而不是 A

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:10:38 33 4
gpt4 key购买 nike

我正在尝试创建一个简单的解析器,它使用 boost::spirit::x3 获取两个可能字符之一。 .问题是 x3::char_('#') | x3::char_('.')似乎具有 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使用然后它可以转换为 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';
}

最佳答案

你是对的,它应该是兼容的,事实上它已经是:

auto const& attr = x3::_attr(ctx);
char c;
x3::traits::move_to(attr, c);

Spirit 的属性兼容性规则(加上自定义点)是承载此类语义的载体。

我同意你的看法,如果属性类型可以直接简化为 char,这可能是一个很好的可用性改进,但我认为这会在未来产生更深远的影响。

关于c++ - boost spirit x3 (A | AN) 属性类型是 variant<A, ?> 而不是 A,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42153829/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com