【发布时间】:2017-01-07 09:51:31
【问题描述】:
下面的代码来自Boost.Spirit x3 documentation。它使用了一种我以前从未见过的有趣的 C++ 语法,如果不知道正确的术语,几乎不可能在搜索查询中描述它。这是类前向声明的简写吗? C++ 标准中哪里提到了这个特性?
namespace parser
{
using x3::eps;
using x3::lit;
using x3::_val;
using x3::_attr;
using ascii::char_;
auto set_zero = [&](auto& ctx){ _val(ctx) = 0; };
auto add1000 = [&](auto& ctx){ _val(ctx) += 1000; };
auto add = [&](auto& ctx){ _val(ctx) += _attr(ctx); };
// What is this? This is the very first use of the identifier `roman`.
x3::rule<class roman, unsigned> const roman = "roman";
// ^^^^^^^^^^^
auto const roman_def =
eps [set_zero]
>>
(
-(+lit('M') [add1000])
>> -hundreds [add]
>> -tens [add]
>> -ones [add]
)
;
BOOST_SPIRIT_DEFINE(roman);
}
【问题讨论】:
-
@BarrettAdair,这也声明了这个类。
-
FWIW,这个功能对于标签来说真的很方便:
using MyType = TaggedThing<struct UniqueTag>;标签对于使每个都成为新类型很有用,这对于使该类型别名一行很有用。将struct放在我的个人偏好中 - 可能是class、union、enum或enum class(和变体)AFAIK。 -
令人着迷。我无法想象编写 C++ 解析器会有多痛苦。谢谢大家!
-
这里还有一个关于详细类型说明符的部分:stackoverflow.com/documentation/c%2b%2b/4891/keywords/18504/…
标签: c++ templates c++11 language-lawyer boost-spirit