通过类模板参数推导,我们可以编写:
std::less Fn;
然而,G++ 8.2 拒绝此代码:
#include <algorithm>
#include <vector>
#include <functional>
int main()
{
std::vector v= { 1, 3, 2, 7, 5, 4 };
std::sort(v.begin(),v.end(),std::greater());
}
发出以下错误:
error: cannot deduce template arguments for 'greater' from ()
Clang++ 7.0 和 MSVC 15.8.0 编译它时不会出现警告。哪个编译器是正确的?
海湾合作委员会是错误的。已经有一个错误报告 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=81486.
[dcl.type.simple]/2 http://eel.is/c++draft/dcl.type.simple#2 says:
A type-specifier of the form typename
opt nested-name-specifieropt template-name is a placeholder for a deduced class type ([dcl.type.class.deduct]).
And [dcl.type.class.deduct]/2 http://eel.is/c++draft/dcl.type.class.deduct#2 says:
推导类类型的占位符也可以用在类型说明符序列 in the 新类型 ID or type-id of a 新表达, as the 简单类型说明符在显式类型转换(函数符号)中([expr.type.conv]),或作为类型说明符 in the 参数声明 of a 模板参数。推导类类型的占位符不应出现在任何其他上下文中。
这种用途是允许的。
[温度参数]/4 http://eel.is/c++draft/temp.arg#4描述了语法错误 that a 模板 ID是必需的,但是没有<>
。然而这里std::greater
没有解决为模板 ID因此该款不适用。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)