Boost Spirit x3无法编译 [英] Boost Spirit x3 not compiling

查看:88
本文介绍了Boost Spirit x3无法编译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在关注boost网站上的x3文档,并且我尝试使用后面的注释示例中解释的内容来扩展有关如何组织代码的示例. 编译项目时(使用g ++或MSVC)时出现以下错误:

I'm following the x3 documentation on the boost website, and I've tried to augment the example on how to organize code with the stuff explained in the annotations example that comes after. I'm having the following error when compiling the project (either with g++ or MSVC):

错误:没有匹配的函数可以调用'boost :: spirit :: x3 :: unused_type :: get()'

error: no matching function for call to 'boost::spirit::x3::unused_type::get()'

在函数on_success的第一行中,以下代码:

In the first line of function on_success in the following code:

// tag used to get the position cache from the context
struct annotate_position
{
    template <typename T, typename Iterator, typename Context>
    inline void on_success(const Iterator &first, const Iterator &last, T &ast, const Context &context)
    {
        auto &position_cache = x3::get<position_cache_tag>(context).get();
        position_cache.annotate(ast, first, last);
    }
};

代码可在此处找到: Github .

注释示例几乎是完全相同的代码,只是在一个文件中,所以我无法弄清楚出了什么问题...

The annotations example is almost the exact same code, just in a single file, so I can't figure out what's wrong...

推荐答案

注释使用with<>指令.这会修改这些规则的上下文.

The annotation uses the with<> directive. That modifies the context for those rules.

但是,上下文已在config.hpp中进行了硬编码,因为这样可以使规则定义在其各自的翻译单元(源文件)中分开.

The context, however, had been hard-coded in config.hpp because that enables the rule definitions to be separated in their own translation unit (source file).

直接修复:

struct position_cache_tag;
using position_cache = boost::spirit::x3::position_cache<std::vector<iterator_type>>;

using simple_context_type = x3::phrase_parse_context<x3::ascii::space_type>::type;

using context_type = boost::spirit::x3::context<
    client::parser::position_cache_tag,
    std::reference_wrapper<position_cache>, 
    simple_context_type
>;

那应该已经可以了.但是,您会发现其他东西丢失了,因为main中的旧parse函数(没有位置注释)也仍然存在.原则上添加像这样的三元组

That should already work. However, you'll find other things missing because the old parse function in main (without position annotations) was also still there. In principle adding a triplet like

BOOST_SPIRIT_INSTANTIATE(person_type, iterator_type, simple_context_type)
BOOST_SPIRIT_INSTANTIATE(employee_type, iterator_type, simple_context_type)
BOOST_SPIRIT_INSTANTIATE(employees_type, iterator_type, simple_context_type)

足以减轻负担,但是,显然,on_success中的注释代码将无法编译.如果需要,您可以摆脱困境,但我只是从main.cpp中删除了未使用的代码.

would be enough to alleviate, but then, obviously, the annotation code in on_success will not compile. If you want, you can SFINAE yourself out of that mess, but I just removed the unused code from main.cpp.

作为奖励,由于我看到您使用的是Boost 1.70,因此这些天您可以不用reference_wrapper进行操作.

As a bonus, you can do without the reference_wrapper these days, since I see you're using Boost 1.70.

过去使用指令需要可变状态才能使用引用包装,但我最近发现了这个错误(

The reference-wrapper used to be required for mutable state in with directives, but I found out recently (Spirit X3, Is this error handling approach useful?) that that's no longer required. Therefore you can simplify the context:

using context_type = boost::spirit::x3::context<
    client::parser::position_cache_tag,
    position_cache, 
    simple_context_type
>;

然后将引用包装器放在两端:

And drop the reference wrapper on both ends:

auto &position_cache = x3::get<position_cache_tag>(context); // NOTE: no more .get()

还有

auto const parser =
    with<position_cache_tag>(positions)[client::employees()];

完整代码(Github)

这是我的工作代码: https://github.com/sehe/corrupted-spirit按顺序包含以下提交,因此很容易找到导致更改的原因:

Full Code (Github)

Here's my working code: https://github.com/sehe/corrupted-spirit containing the following commits in order, so it's easy to find what changed why:

commit 2d1d553afab53d7a83620406c2dcd50967bf2765
Date:   Wed Jul 31 22:50:49 2019 +0200

    Build tweaks

    Make it compile on my linux box, and adding some minimum
    debug/sanitizer/diagnostics flags

commit 98a989bb165d0b25b6919449d4dd09f7656168c8
Date:   Wed Jul 31 22:51:50 2019 +0200

    Various compiler wanrings, no impact

commit 91f5c607c10a489e2d7b9e45dca55438d05419a2
Date:   Wed Jul 31 22:53:46 2019 +0200

    Fixed style issues in main.cpp

     - using namespace (my first hunch was with `ref` being std::ref instead
     of boost::ref, but that turned out a red herring. Better to be explicit
     though

     - added condition on use of ast[1]

commit 084700c80023d4fb291bee36f41cb99f23f7dffa
Date:   Wed Jul 31 22:51:20 2019 +0200

    Fix the context_type in config.hpp

commit df7f9505e042b93bcd62167090e89008788218de (HEAD -> master, sehe/master)
Date:   Wed Jul 31 22:56:20 2019 +0200

    Simplify the with directive

    1.70.0 no longer requires manual ref() for with directives with mutable
    context items.

这篇关于Boost Spirit x3无法编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆