Boost.Spirit使用的这个不寻常的C++模板功能的名称是什么?

Bar*_*air 51 c++ templates boost-spirit language-lawyer c++11

下面的代码来自Boost.Spirit x3文档.它使用了我以前从未见过的有趣的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);
}
Run Code Online (Sandbox Code Playgroud)

Rob*_*ost 41

模板的参数不一定必须定义才能使用.使用"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;
}
Run Code Online (Sandbox Code Playgroud)

正如人们在上面的评论中所指出的,这区分了模板的这种实例化.要直接回答您的问题,在模板实例化中指定模板参数的性质称为"详细类型说明符".


M.M*_*M.M 27

它与:

class roman;

x3::rule<roman, unsigned> const roman = "roman";
Run Code Online (Sandbox Code Playgroud)

换句话说,写入class T预期类型T名称的位置,首先声明它是类的名称,然后继续T作为用于表达式其余部分的类型名称.

请注意,在C++中,typename roman和在roman此声明的变量名之间没有冲突; 这是允许的.


另一种情况可以在没有模板的情况下发生,例如:

void func( class bar *ptr );
Run Code Online (Sandbox Code Playgroud)

如果bar未申报则是正确的; 它声明bar然后声明函数以获取指针bar.