dvd*_*dvd 8 c++ boost-spirit boost-spirit-x3
此代码不编译(gcc 5.3.1 + boost 1.60):
#include <boost/spirit/home/x3.hpp>
namespace x3 = boost::spirit::x3;
template <typename T>
void parse(T begin, T end) {
auto dest = x3::lit('[') >> x3::int_ >> ';' >> x3::int_ >> ']';
auto on_portal = [&](auto& ctx) {};
auto portal = (x3::char_('P') >> -dest)[on_portal];
auto tiles = +portal;
x3::phrase_parse(begin, end, tiles, x3::eol);
}
int main() {
std::string x;
parse(x.begin(), x.end());
}
Run Code Online (Sandbox Code Playgroud)
它失败并带有静态断言:
error: static assertion failed: Attribute does not have the expected size.
Run Code Online (Sandbox Code Playgroud)
感谢wandbox我也尝试了boost 1.61和clang,两者都产生了相同的结果.
如果我删除附加的语义动作portal
,它编译得很好; 如果我dest
改为:
auto dest = x3::lit('[') >> x3::int_ >> ']';
Run Code Online (Sandbox Code Playgroud)
任何帮助,将不胜感激.TIA.
这也让我感到惊讶,我会在邮件列表(或错误跟踪器)中将其报告为潜在错误。
同时,您可以通过提供以下属性类型来“修复”它dest
:
#include <boost/fusion/adapted/std_tuple.hpp>
#include <boost/spirit/home/x3.hpp>
#include <iostream>
namespace x3 = boost::spirit::x3;
template <typename T>
void parse(T begin, T end) {
auto dest = x3::rule<struct dest_type, std::tuple<int, int> > {} = '[' >> x3::int_ >> ';' >> x3::int_ >> ']';
auto on_portal = [&](auto& ctx) {
int a, b;
if (auto tup = x3::_attr(ctx)) {
std::tie(a, b) = *tup;
std::cout << "Parsed [" << a << ", " << b << "]\n";
}
};
auto portal = ('P' >> -dest)[on_portal];
auto tiles = +portal;
x3::phrase_parse(begin, end, tiles, x3::eol);
}
int main() {
std::string x = "P[1;2]P[3;4]P[5;6]";
parse(x.begin(), x.end());
}
Run Code Online (Sandbox Code Playgroud)
印刷:
Parsed [1, 2]
Parsed [3, 4]
Parsed [5, 6]
Run Code Online (Sandbox Code Playgroud)
注意我更改
char_('P')
为只是lit('P')
因为我不想使处理属性中的字符的示例复杂化。无论如何,也许您并不打算将其包含在公开属性中。
归档时间: |
|
查看次数: |
255 次 |
最近记录: |