我正在关注Boost网站上的X3文档,并且我试图增强有关如何与以下注释示例中解释的内容组织代码的示例。编译项目时,我有以下错误(使用G 或MSVC(:
错误:呼叫'boost :: spirit :: x3 :: unused_type :: get(('
没有匹配函数
在以下代码中的第一行函数on_success中:
// 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。
注释示例几乎是一个完全相同的代码,仅在一个文件中,所以我不知道怎么了...
注释使用with<>
指令。这修改了这些规则的上下文。
但是,上下文已在config.hpp
中进行了刻板编码,因为这使得规则定义可以在其自己的翻译单元(源文件(中分开。
直接修复它:
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
功能(无位置注释(仍然存在。原则上添加了一个
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
中删除了未使用的代码。
奖金
作为奖励,您可以在没有reference_wrapper
的情况下做,因为我看到您使用的是Boost 1.70。
曾经使用指令的可变状态所需的参考 - wrapper,但我最近发现(Spirit X3,这种错误处理方法是否有用吗?(,这是不再需要的。因此,您可以简化上下文:
using context_type = boost::spirit::x3::context<
client::parser::position_cache_tag,
position_cache,
simple_context_type
>;
并将参考包装器放在两端:
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,其中包含以下提交的顺序,因此很容易找到改变原因:
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.