Talk:cpp/language/lambda

"mutable: allows body to modify the parameters captured by copy, and to call their non-const member functions" wrong, Object, i.e. captures, not parameters?!
 * Fixed. Thanks for the catch, and sorry for the delay. --D41D8CD98F (talk) 23:41, 9 March 2020 (PDT)

I have some changes I'd like to make (or seen made) but I want to open it up for feedback first rather than possibly having the changes rolled back.

First, I'd like to start the description with something simpler, along the lines of "A Lambda Function is an anonymous (inline and unnamed) function called via function pointer or functor.

Next, I think the first part of the example might be too complicated, obscuring the role of the lambda part. I suggest that there is first a more basic loop with a traditional functor, then a similar task done via a lambda function.

The last part of the example, as it is, seems like it will be confusing to some as std::function is newer, and not obvious, the example might seem like a just a complicated way to code: int func(int i) {return i+4;}

Arbalest 09:11, 1 November 2012 (PDT)
 * I feel that such description could be misleading: it would be introducing the general programming concept 'lambda function', not the C++ 'lambda expression' or the C++ 'closure object'. It could be done (and probably should be done, eventually, seeing as the title of this page is 'lambda functions', which are never mentioned in the C++ standard or in the body of the page), but it would have to explain the difference first. The example currently is as basic as it gets: lambdas were made specifically to make algorithms easier to use (a loop would be neither simple nor typical), and std::function is the most common way to manipulate a closure. If anything, it could use a couple more algorithms, especially ones that make use of lambda captures. --Cubbi 10:43, 1 November 2012 (PDT)
 * I also don't see a lot of benefit to the description change, but I do think that we could start off with an even simpler example -- specifically, one that doesn't use the erase-remove idiom. How about we start with using std::count_if to count the number of numbers in the vector that match some predicate, and then continue with the rest of the current example? -Nate 11:42, 1 November 2012 (PDT)
 * By loop I mean for_each(begin, end, functor) followed by for_each(begin, end, lambda-exp). Arbalest 15:29, 1 November 2012 (PDT)
 * I think a std::for_each example would also work fine. If we use both a functor and a lambda-exp, it would be good to make sure that it's clear that the functor is just there for juxtaposition and isn't required to make the lambda work. -Nate 12:37, 2 November 2012 (PDT)

There needs to be some documentation on the use of the +[] syntax for converting lambdas to function pointers in some circumstances. 216.239.45.95 14:17, 2 May 2014 (PDT)Mark
 * Do you mean when assigning to or using other forms of template argument deduction? You could also use the  syntax, operator+ isn't all that special. But sure, it's a nice bit of trivia to mention (along with all the other work this page needs) --Cubbi (talk) 15:15, 2 May 2014 (PDT)

Formatting Issue
There is a small formatting issue in the box with the header ClosureType::operator ret(*)(params)

There are several nested boxes inside, and at the deepest nesting level on the right, only the text "(s" is visible which should probably read "(since C++14)"

Mwe (talk) 23:52, 30 June 2015 (PDT)


 * I put an MCVE on my user page to demo this issue. Hoping a CSS guru can see what's up. Oktal (talk) 10:33, 30 July 2016 (PDT)


 * This seems to have been fixed now. Oktal (talk) 11:16, 25 November 2018 (PST)

Static Variable Initialized To Lambda
I believe it would be wise to mention the danger of using a static auto variable initialized to a lambda with auto duration captures on this page. The bugs it produces can be very subtle and possibly compromise system integrity and security. The issue has been discussed at Stack Overflow here and here. It is not made terribly obvious that the lambda expression will only capture in it's evaluated context, as opposed to a general contract in the scope of reference.

2607:FEA8:BD1F:FFAD:A82E:F6B:ECEF:D5C1 22:16, 9 June 2018 (PDT)

Attributes
It would be nice to give an examples of correct attribute usage on lambdas, see https://stackoverflow.com/questions/26888805/how-to-declare-a-lambdas-operator-as-noreturn

You can have attirbutes, but not the ones you expect (notably not [[noreturn]] and not [[gnu::always_inline]], at least as per clang).

Error in viewing on my smartphone
Please search for no specialization can be constexpr because of static s When improved, please delete this comment. Thanks a lot, K. Karamazen
 * Could you recheck now? If there are still rendering problems I'd better undo my recent formatting updates on the page.--Space Mission (talk) 10:55, 14 March 2021 (PDT)