我正在跟踪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。
注释示例几乎是完全相同的代码,只是在一个文件中,所以我不知道出了什么问题.
发布于 2019-07-31 21:10:14
注释使用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中的旧main函数(没有位置注释)仍然存在。原则上添加一个三重奏,如
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。
引用包装器曾经是可变状态的必要条件,但我最近发现(精神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.https://stackoverflow.com/questions/57262850
复制相似问题