以下所有标准引用均指current ISO Standard Working Draft , 生成于 2020-06-22。
[dcl.fct]/18指出 [提取,重点矿]:
An abbreviated function template is a function declaration that has one or more generic parameter type placeholders ([dcl.spec.auto]). An abbreviated function template is equivalent to a function template ([temp.fct]) whose template-parameter-list includes one invented type template-parameter for each generic parameter type placeholder of the function declaration, in order of appearance. [...]
这样函数声明的以下内容可能是等效的:
template <typename T>
void f(T);
void f(auto); // re-declaration
然而,我们可能注意到 [dcl.fct]/18 的例子指出[...]
These declarations are functionally equivalent (but not equivalent) to the following declarations.
[...]
可以说(我不确定如何解释这一点)与前面段落中的等价陈述相冲突。
现在,GCC 10.1.0 和 Clang 10.0.0(以及 GCC:HEAD 和 Clang:HEAD)在这里都有一些混合行为。如果我们声明一个函数模板,然后使用混合的经典函数模板语法和缩写函数模板语法来定义它(/重新声明它),Clang 接受大多数情况(定义先前声明的函数)而 GCC 拒绝所有情况(参见) 重新声明为单独声明的函数,随后在重载解析中出现歧义失败):
// A1: Clang OK, GCC error
template <typename T>
void a(T);
void a(auto) {}
// B1: Clang OK, GCC error
void b(auto);
template <typename T>
void b(T) {}
// C1: Clang OK, GCC error
template <typename T, typename U>
void c(T, U);
void c(auto, auto) {}
// D1: Clang OK, GCC error
template <typename T, typename U>
void d(T, U);
template <typename T>
void d(T, auto) {}
// E1: Clang error, GCC error
template <typename T>
void e(T, auto);
template <typename T>
void e(auto, T) {}
int main() {
a(0); // Clang OK, GCC error.
b(0); // Clang OK, GCC error.
c(0, '0'); // Clang OK, GCC error.
d(0, '0'); // Clang OK, GCC error.
e(0, '0'); // Clang error, GCC error.
}
奇怪的是,如果我们将函数模板设为类成员函数模板,GCC 和 Clang 都接受案例 A1 通过 D1 , 但两者都拒绝最后的情况 E1 多于:// A2: OK
struct Sa {
template <typename T>
void a(T);
};
void Sa::a(auto) {}
// B2: OK
struct Sb {
void b(auto);
};
template <typename T>
void Sb::b(T) {}
// C2: OK
struct Sc {
template <typename T, typename U>
void c(T, U);
};
void Sc::c(auto, auto) {}
// D2: OK
struct Sd {
template <typename T, typename U>
void d(T, U);
};
template <typename T>
void Sd::d(T, auto) {}
// E2: Error
struct Se {
template <typename T>
void e(T, auto);
};
template <typename T>
void Se::e(auto, T) {}
带有以下错误消息:GCC
error: no declaration matches 'void Se::e(auto:7, T)' note: candidate is: 'template<class T, class auto:6> void Se::e(T, auto:6)'
Clang
error: out-of-line definition of 'e' does not match any declaration in 'Se'
现在,类型模板参数的名称不需要与函数模板的重新声明(或定义)保持一致,因为只需命名泛型类型占位符。
GCC 的错误消息特别有趣,暗示发明的类型模板参数被视为具体类型而不是泛型类型占位符。
问题:
最佳答案
这:
template <typename T> void e(T, auto);
翻译为:
template<typename T, typename U>
void e(T, U);
相比之下,这:template <typename T> void e(auto, T) {}
翻译为:
template <typename T, typename U>
void e(U, T) {}
请记住 abbreviated function template parameters are placed at the end of the template parameter list .因此,由于颠倒了模板参数的顺序,它们并没有声明相同的模板。第一个声明一个模板,第二个声明并定义一个不同的模板。您不会因此而得到编译错误,因为第二个定义也是一个声明。但是,当您使用类成员时,成员外定义不是声明。因此,它们必须具有匹配的成员内声明。他们没有;因此错误。
至于其他,“功能等效(但不等效)”文本是非规范性符号。您引用的实际规范性文本明确指出这些是“等效的”,而不仅仅是“功能等效”。并且由于术语“等效”,根据 [temp.over.link]/7 , 用于匹配声明和定义,在我看来,标准规定 A 到 D 的情况很好。
奇怪的是,这个非规范文本是 introduced by the same proposal that introduced the normative text .但是,它继承了
ConceptName auto
的建议。来自 seems clear that it means "equivalent", not "functionally equivalent" 的语法.因此,就规范性文本而言,一切似乎都很清楚。但是,非规范性矛盾的存在表明存在编辑问题或规范中的实际缺陷。
虽然标准本身在措辞方面是明确的并且在规范上是合理的,但这似乎不是标准作者的意图。
P0717引入了“功能等效”的概念,以区别于“等效”。该提议被接受。但是,P0717 是在采用 C++20 的概念 TS 的早期引入的。在该提案中,它特别谈到了简洁的模板语法,并且 EWG 明确投票赞成采用“功能等效”的措辞,而不是概念 TS 的“等效”措辞。
也就是说,P0717 明确指出,委员会打算要求用户使用一致的语法。
但是,Concepts TS 中的简洁模板语法已从 C++20 中删除(或者更确切地说,从未真正添加)。这意味着任何“功能等效”的措辞都从未出现过,因为该功能从未出现过。
然后发生了 P1141,它添加了缩写模板语法,涵盖了 Concepts TS 简洁模板语法的大部分基础。但是,尽管 P0717 的作者之一是 P1141 的作者,但显然有人在措辞上犯了错误,没有人注意到它。这可以解释为什么非规范性文本指出缺乏真正的对等:因为这实际上是委员会的意图。
因此,这很可能是规范性文本中的错误。
关于c++ - 函数模板和缩写函数模板之间的等价关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63525200/