Spirit X3,语义操作使编译失败,原因是:属性没有预期的大小 [英] Spirit X3, semantic action makes compilation fails with: Attribute does not have the expected size
问题描述
此代码无法编译(gcc 5.3.1 + boost 1.60):
This code does not compiles (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());
}
它以静态断言失败:
error: static assertion failed: Attribute does not have the expected size.
多亏了wandbox,我也尝试了boost 1.61和clang,它们都产生了相同的结果.
Thanks to wandbox I also tryied boost 1.61 and clang, both produce the same results.
如果我删除了portal
附带的语义动作,则可以正常编译;如果将dest
更改为:
If I remove the semantic action attached to portal
, it compiles fine; the same happens if I change dest
to:
auto dest = x3::lit('[') >> x3::int_ >> ']';
任何帮助将不胜感激. TIA.
Any help would be appreciated. TIA.
推荐答案
这也令我感到惊讶,我在邮件列表(或错误跟踪器)中将其报告为潜在的错误.
This is surprising to me too, I'd report it at the mailing list (or the bug tracker) as a potential bug.
同时,您可以通过提供dest
的属性类型来修复"它:
Meanwhile, you can "fix" it by supplying an attribute type for 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());
}
打印:
Parsed [1, 2]
Parsed [3, 4]
Parsed [5, 6]
注意,我将
char_('P')
更改为lit('P')
,因为我不想使处理属性中字符的示例复杂化.也许您并不是故意要将它包含在暴露的属性中.
NOTE I changed
char_('P')
into justlit('P')
because I didn't want to complicate the sample dealing with the character in the attribute. Perhaps you didn't mean to have it in the exposed attribute anyways.
这篇关于Spirit X3,语义操作使编译失败,原因是:属性没有预期的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!