明确指定通用lambda的operator()模板参数是否合法? [英] Is it legal to explicitly specify a generic lambda's operator() template arguments?

查看:69
本文介绍了明确指定通用lambda的operator()模板参数是否合法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否符合以下C ++代码标准?

Is the following C++ code standard compliant?

#include <iostream>

int main()
{
    [](auto v){ std::cout << v << std::endl; }.operator()<int>(42);
}

clang ++ 3.8.0 g ++ 7.2.0 可以很好地编译此代码(编译器标志为-std=c++14 -Wall -Wextra -Werror -pedantic-errors).

Both clang++ 3.8.0 and g++ 7.2.0 compile this code fine (the compiler flags are -std=c++14 -Wall -Wextra -Werror -pedantic-errors).

推荐答案

这确实符合标准.该标准指定必须有一个成员operator(),并且在其参数声明子句中对于auto的每次出现都具有一个模板参数.没有任何措辞禁止明确提供这些内容.

This is indeed standard compliant. The standard specifies there must be a member operator(), and that it has one template argument for every occurence of auto in its paramater-declaration-clause. There is no wording that forbids providing those explicitly.

这行的底线:lambda的调用运算符只是一个普通函数(模板,如果是通用的话).

Bottom of the line: The call operator of a lambda is just a normal function (template, if generic).

相关标准条款供参考:

非泛型lambda表达式的闭包类型有一个public 内联函数调用运算符(16.5.4),其参数和返回值 类型由lambda表达式的 参数声明子句和尾随返回类型. 对于通用lambda,闭包类型具有公共内联函数 呼叫操作员成员模板(17.5.2),其template-parameter-list 包含一个发明的类型模板-每次出现的参数 Lambda的参数声明子句中的auto的顺序为 .发明的类型模板参数是参数包 如果相应的参数声明声明了一个函数 参数包(11.3.5).返回类型和函数参数 函数调用运算符模板是从 lambda表达式的尾随返回类型和 通过替换每次出现在auto中的参数声明子句 名称为parameter-declaration-clause的decl-specifiers 发明的相应模板参数的值.

The closure type for a non-generic lambda-expression has a public inline function call operator (16.5.4) whose parameters and return type are described by the lambda-expression’s parameter-declaration-clause and trailing-return-type respectively. For a generic lambda, the closure type has a public inline function call operator member template (17.5.2) whose template-parameter-list consists of one invented type template- parameter for each occurrence of auto in the lambda’s parameter-declaration-clause, in order of appearance. The invented type template-parameter is a parameter pack if the corresponding parameter-declaration declares a function parameter pack (11.3.5). The return type and function parameters of the function call operator template are derived from the lambda-expression’s trailing-return-type and parameter-declaration-clause by replacing each occurrence of auto in the decl-specifiers of the parameter-declaration-clause with the name of the corresponding invented template-parameter.

N4659(C ++ 17)中的8.1.5.1/3 [expr.prim.lambda.closure],请强调我的.

8.1.5.1/3 [expr.prim.lambda.closure] in N4659 (C++17), emphasize mine.

这篇关于明确指定通用lambda的operator()模板参数是否合法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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