c++ - §14/2 中的 "last component"一词的含义是什么?

标签 c++ templates c++11 language-lawyer c++14

§14/2(强调我的)

A template-declaration can appear only as a namespace scope or class scope declaration. In a function template declaration, the last component of the declarator-id shall not be a template-id. [ Note: That last component may be an identifier, an operator-function-id, a conversion-function-id, or a literal-operator-id. In a class template declaration, if the class name is a simple-template-id, the declaration declares a class template partial specialization (14.5.5). —end note ]

请注意,declarator-id 在语法 (N4140) 中定义,如下所示:

声明符 ID:
...选择   id 表达式

id-表达式:
不合格的 ID
合格 ID

不合格 ID:
标识符
操作符函数 ID
转换函数ID
文字运算符 ID
~类名
~ decltype-specifier
模板 ID

鉴于在函数模板声明中,declarator-id 是一个标识符、operator-function-idconversion-function-id ,或者一个literal-operator-id,上面的表达式“最后一个组件”是什么意思?

最佳答案

在语法引用中,您省略了 qualified-id 的产生式,它扩展为 unqualified-id 序列,以及最后一个组件 指的是最后一个 unqualified-id

标准告诉您 函数模板声明不能以template-id结尾 与类模板的部分特化相反

template<typename T> class A {};
template<typename T> class A<std::vector<T> > {}; // ends with simple-template-id

即使函数模板的qualified-id的某些组件可能是template-id,如

template<typename>
struct A {
    void f();
};
template<typename T>
void A<T>::f() {} // cannot end with template-id

关于c++ - §14/2 中的 "last component"一词的含义是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28203734/

相关文章:

c++ - 数组中最近点的索引,每个点包含3个元素

c++ - 我可以只对一部分代码启用矢量化吗?

c++ - 在 std::map 中存储结构实例

c++ - 无法删除不需要的重载

c++ - 有什么方法可以访问除最后一个模板参数之外的所有内容吗?

c++ - 初始化一个 unique_ptr 数组

c++ - 回调函数和常规函数有什么区别?

c++ - 在同一赋值语句中将多个字符串文字转换为 const char*

c++ - 函数指针类型和值的部分类特化

c++ - 将左值传递给右值