C++ 这个不寻常的C++;Boost.Spirit使用的模板功能?

C++ 这个不寻常的C++;Boost.Spirit使用的模板功能?,c++,templates,c++11,language-lawyer,boost-spirit,C++,Templates,C++11,Language Lawyer,Boost Spirit,下面的代码来自。它使用了我以前从未见过的有趣的C++语法,这几乎不可能在搜索查询中描述而不知道正确的术语。这是类的前向声明的缩写吗?C++标准中提到的这个特性在哪里? namespace parser { using x3::eps; using x3::lit; using x3::_val; using x3::_attr; using ascii::char_; auto set_zero = [&](auto& ctx){

下面的代码来自。它使用了我以前从未见过的有趣的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);
}
名称空间解析器
{
使用x3::eps;
使用x3::lit;
使用x3::_val;
使用x3::_attr;
使用ascii::char;
自动设置_zero=[&](auto&ctx){u val(ctx)=0;};
自动添加1000=[&](自动和ctx){u val(ctx)+=1000;};
自动添加=[&](自动和ctx){u val(ctx)+=\u attr(ctx);};
//这是什么?这是第一次使用标识符“roman”。
x3::rule const roman=“roman”;
//       ^^^^^^^^^^^
自动常数罗马定义=
每股收益[设定为零]
>>
(
-(+lit('M')[add1000])
>>-数百[加]
>>-十[加]
>>-一[加]
)
;
提升精神定义(罗马);
}

模板的参数不一定要定义才能使用。“class roman”的使用实际上声明了class roman

下面是一些示例代码:

#include <iostream>
template <class T> void foo();
template<> void foo<class roman>()
{
    // allowed because roman is declared
    roman* pointer1;
    // not allowed because romania is not declared
    // romania* pointer2;
    std::cout << "Hello world!" << std::endl;
    return;
}
int main(int argc, char** argv) {
    return 0;
}
#包括
模板void foo();
模板void foo()
{
//允许,因为声明了roman
罗马*指针1;
//不允许,因为罗马尼亚未申报
//罗马尼亚*指针2;
标准::cout它与:

class roman;

x3::rule<roman, unsigned> const roman = "roman";

如果
bar
未声明,则该函数是正确的;它声明
bar
,然后声明函数以获取指向
bar

的指针以简化搜索:@barretdair,它也声明了类。FWIW,此功能对于标记非常方便:
使用MyType=TaggedThing;
标记对于使每个类都成为一个ew类型,这对于使该类型别名为一行非常有用。放置
struct
是我个人的偏好-它可以是
class
union
enum
,或
enum class
(和变体)AFAIK.Fascinating。我无法想象编写C++解析器是多么痛苦。谢谢大家!这里还有一个关于详细类型说明符的章节:谢谢罗伯特的讲解例子。我找到了从上面的克里斯评论中找到相关C++标准部分的方法。在C++ 11中,这是ISO/IEC 1488~2011年中描述的。
void func( class bar *ptr );