Is it legal to specify general arguments to a lambda operator () pattern?

Does the following C ++ code conform to the standard?

#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 in order (compiler flags -std=c++14 -Wall -Wextra -Werror -pedantic-errors ).

+7
c ++ language-lawyer lambda templates generic-lambda
source share
2 answers

Yes, it seems to be clearly defined , since the template parameters for lambdas operator() strictly defined.

[expr.prim.lambda]/5

...
For a common lambda, the closure type has a public built-in function (14.5.2), whose parameter list template consists of one invented parameter template type for each occurrence of auto in the lambdas-declaration-clause parameter, in the order of appearance.
...

+10
source share

This is truly a standard match. The standard states that there must be a member of operator() , and that it has one template argument for each occurrence of auto in its paramater-declaration clause. There is no wording prohibiting them explicitly.

At the bottom of the line: the lambda call statement is just a normal function (template, if common).


For reference, the corresponding standard offer:

The closure type for a non-generic lambda expression has an inline function call (16.5.4), the parameters and return of which type are described by the lambda expressions param-declaration-clause and trailing-return-type, respectively. For a common lambda, the closure type has a publicly available built-in function, the call operator operator template (17.5.2), the list of template parameters consists of one invented type of the template parameter for each auto case in the lambdas-declaration-clause parameter, in appearance order. A recognized type template type is a parameter package if the corresponding parameter declaration declares the function parameter package (11.3.5). The return type and function parameters the function operator operator template is derived from the lambda expression trailing-return-type and the parameter-declaration-sentence, replacing each occurrence of auto in the declaration specifiers of the parameter-declaration-sentence with the name of the corresponding invented parameter template.

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

+10
source share

All Articles