Why does a default argument for a lambda argument

2020-06-01 04:28发布

问题:

I had this lambda somewhere in my code:

[](bool a, bool=true){ return !a;} }

and GCC 4.6 "complained" with this warning:

warning: default argument specified for lambda parameter [-pedantic]

Which is mightily unhelpful when you don't know why this is "bad". I consulted the FDIS n3290 and didn't find anything in 5.1.2 Lambda Expressions with regards to default arguments and a lambda.

UPDATE: I filed a bug report here.

UPDATE2: OK, from now on I'm using -pedantic-errors. -pedantic only emits warnings, not errors.

回答1:

Section 5.1.2 paragraph 5 specifically says that you can not have default values for the parameters.

Default arguments (8.3.6) shall not be specified in the parameter-declaration-clause of a lambda-declarator.



回答2:

Since C++14 it is allowed. It was found to be a defect long time ago: Default arguments for lambdas, and also Default arguments in lambda-expressions.



回答3:

It makes no sense to have a default argument in a lambda function -- how could it ever be used? On the other hand, it does no harm, so why not allow it, after emitting a warning?



标签: c++ lambda c++11