使用透明的std函数对象时,我们是否仍需要写空尖括号? [英] Do we still need to write the empty angle brackets when using transparent std function objects?

查看:85
本文介绍了使用透明的std函数对象时,我们是否仍需要写空尖括号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过类模板参数推导,我们可以编写:

With class template argument deduction we can write:

std::less Fn;

但是,G ++ 8.2拒绝此代码:

However, G++ 8.2 rejects this code:

#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会在没有警告的情况下对其进行编译.哪个编译器是正确的?

Clang++ 7.0 and MSVC 15.8.0 compile it without warnings. Which compiler is right?

推荐答案

GCC错误.已经存在错误报告.

GCC is wrong. There is already a bug report.

[dcl.type.simple]/2 说:

类型说明符,格式为typename opt 嵌套名称说明符 opt 模板名称是推导的类类型([dcl.type.class.deduct])的占位符.

A type-specifier of the form typenameopt nested-name-specifieropt template-name is a placeholder for a deduced class type ([dcl.type.class.deduct]).

[dcl.type.class.deduct]/2 说:

推导类类型的占位符也可以在 new-type-id type-id new-expression 作为显式类型转换(功能符号)中的 simple-type-specifier ([expr. type.conv]),或作为 template-parameter parameter-declaration 中的 type-specifier .推导的类类型的占位符不得出现在任何其他上下文中.

A placeholder for a deduced class type can also be used in the type-specifier-seq in the new-type-id or type-id of a new-expression, as the simple-type-specifier in an explicit type conversion (functional notation) ([expr.type.conv]), or as the type-specifier in the parameter-declaration of a template-parameter. A placeholder for a deduced class type shall not appear in any other context.

允许这种使用.

[temp.arg]/4 描述了语法错误,要求提供 template-id ,但没有<>.但是,此处std::greater不能解析为 template-id ,因此该段落不适用.

[temp.arg]/4 describes the syntax error that a template-id is required but there is no <>. However here std::greater is not resolved as a template-id so that paragraph does not apply.

这篇关于使用透明的std函数对象时,我们是否仍需要写空尖括号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆