I think the following code is very handy and no harmful:
auto fn = [](bool b = false) -> int // NOT legal in C++11 { return b ? 1 : 0; };
Why does C++11 explicitly prohibit default arguments of the lambda expression?
I just wonder the rationales and considerations behind.
I want to know "WHY" rather than "WHAT" the C++11 standard says.
In C++11 and later, a lambda expression—often called a lambda—is a convenient way of defining an anonymous function object (a closure) right at the location where it's invoked or passed as an argument to a function.
Generally no, but in gcc You may make the last parameter of funcA() optional with a macro.
Default arguments are only allowed in the parameter lists of function declarations and lambda-expressions, (since C++11) and are not allowed in the declarations of pointers to functions, references to functions, or in typedef declarations.
Which is the correct condition for the default arguments? Explanation: The default arguments must be declared at last in the argument list. This is to ensure that the arguments doesn't create ambiguity.
This seems to be a bug in gcc; the standard permits lambda expressions in default parameters as long as nothing is captured. The following seems to be everything the FDIS says about lambdas in default parameters, so any use other than what is forbidden here ought to be permitted by default.
Since the lambda expressions has been introduced in C++11 standards (2011) – as it that allow you to write an inline, anonymous functor to replace the struct f – still it is not popular in C++ usage among the developers. I try to list some of the reasons, and its disadvantages below,
A lambda-expression appearing in a default argument shall not implicitly or explicitly capture any entity. Thanks for contributing an answer to Stack Overflow! Please be sure to answer the question.
The above code works fine with a recent version of gcc (4.7-20120225). Show activity on this post. Show activity on this post. This seems to be a bug in gcc; the standard permits lambda expressions in default parameters as long as nothing is captured.
There is no real reason that lambdas can't have default arguments. However, there are two main ways to use lambdas, and only one of them would allow default arguments without changing the type system.
You can call the lambda directly, or through a template. Default parameters would work fine in this case.
You can call the lambda through std::function
. Default parameters would not work without changing the type system.
My guess is that new functions that people write in C++11 will usually take std::function
parameters because this way, the function won't have to be a template, or it won't have to be instantiated for every single lambda and functor that gets passed to it.
std::function
(or function pointer) take defaults?It's not obvious what the type of such a function would be.
If it's std::function<int(bool)>
, then how do you call it with the defaults? (You can't.)
If it's std::function<int(bool=false)>
, then what types is it compatible with, and how does conversion work? Can you convert it to std::function<int()>
? What about std::function<int(bool=true)>
?
If it's something new like std::function<int(bool=default)>
, then what types is it compatible with, and how does conversion work?
Basically, this isn't just a switch you can flip in the standard and make function pointers / std::function
handle default arguments. Default arguments in normal functions are handled using information from the function's declaration, which is not available at the call site for a lambda or function pointer. So you would have to encode information about the defaults into the function type, and then work out all of the non-obvious rules for conversion and compatibility.
So you would have to come up with a compelling case for why such a feature would be added, and convince the committee.
I haven't answered this question. But I don't think it would be a very useful feature to add. I would delete this answer if I could, but it's been accepted. I would downvote it if I could, but it's mine. C'est la vie.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With