Spirit X3,语义操作使编译失败,原因是:属性没有预期的大小

编程入门 行业动态 更新时间:2024-10-24 02:29:46
本文介绍了Spirit X3,语义操作使编译失败,原因是:属性没有预期的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

此代码无法编译(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:

在Coliru上直播

#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 just lit('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,语义操作使编译失败,原因是:属性没有预期的大小

本文发布于:2023-07-05 07:02:40,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1034446.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:语义   属性   大小   原因   操作

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!