Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What if the lambda expression of C++11 supports default arguments?

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.

like image 290
xmllmx Avatar asked Jan 07 '13 03:01

xmllmx


People also ask

What is lambda expression in C++11?

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.

Does C language support default arguments?

Generally no, but in gcc You may make the last parameter of funcA() optional with a macro.

Which case default argument passing in function is not allowed?

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?

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.

Why can't I use Lambdas in default parameters?

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.

Why lambda expressions are not popular in C++?

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,

Can a lambda-expression appearing in a default argument capture an entity?

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.

What version of GCC is used for lambda expressions?

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.


1 Answers

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.

  1. You can call the lambda directly, or through a template. Default parameters would work fine in this case.

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

Why can't a 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.

So, why can't lambdas take defaults?

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.

like image 100
Dietrich Epp Avatar answered Sep 30 '22 22:09

Dietrich Epp