Is it legal to explicitly specify a generic lambda's operator() template arguments?

亡梦爱人 提交于 2020-12-08 05:51:00

问题


Is the following C++ code standard compliant?

#include <iostream>

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

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).


回答1:


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.

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


For reference, the relevant standard clause:

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.

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




回答2:


Yes, it appears to be well-defined since template parameters for lambdas' operator() are strictly defined.

[expr.prim.lambda]/5

...
For a generic lambda, the closure type has a public inline function call operator member template (14.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.
...



来源:https://stackoverflow.com/questions/49392738/is-it-legal-to-explicitly-specify-a-generic-lambdas-operator-template-argumen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!