Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does a default argument for a lambda argument trigger a -pedantic" GCC warning?

Tags:

c++

c++11

lambda

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.

like image 494
rubenvb Avatar asked Jun 08 '11 15:06

rubenvb


People also ask

How do I get rid of the unused variable warning?

Solution: If variable <variable_name> or function <function_name> is not used, it can be removed. If it is only used sometimes, you can use __attribute__((unused)) . This attribute suppresses these warnings.

What is stateless lambda c++?

A stateless lambda is a lambda that captures nothing from its environment. Or to put it the other way around. A stateless lambda is a lambda, where the initial brackets [] in the lambda definition are empty. For example, the lambda expression auto add = [ ](int a, int b) { return a + b; }; is stateless.

What is capture clause in lambda function in c++?

Capture clause A lambda can introduce new variables in its body (in C++14), and it can also access, or capture, variables from the surrounding scope. A lambda begins with the capture clause. It specifies which variables are captured, and whether the capture is by value or by reference.

How to capture a variable c++ lambda?

Much like functions can change the value of arguments passed by reference, we can also capture variables by reference to allow our lambda to affect the value of the argument. To capture a variable by reference, we prepend an ampersand ( & ) to the variable name in the capture.


1 Answers

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.

like image 112
Bo Persson Avatar answered Sep 22 '22 12:09

Bo Persson