3 ms·
I've also found lambdas less life-changing than I'd hoped. In a lot of languages I use short single-expression closures all over the place, but C++11's lambda s
by plorkyeran 14y ago
I've also found lambdas less life-changing than I'd hoped. In a lot of languages I use short single-expression closures all over the place, but C++11's lambda syntax is too verbose for that to work well. The difference between "o -> o.bar" (in C#) and "[](FooObject *o) { return o.bar; }" may seem somewhat superficial, but it greatly cuts down on how often the lambda is the natural and clean way to do something.
OTOH, having local functions with upvalues was a benefit of lambdas I hadn't even originally considered, but that I've found myself delighting in.
- agwa 14y agoYeah they are verbose (like so much in C++ sadly). For frequently recurring patterns like "return o.bar" you can write a templated helper function that returns a functor which you can use like this: getmem(&FooObject::bar) It's longer than the C# version but shorter than the lambda and easier to type because your editor can auto-complete most of it (unlike the lambda which has a lot of syntax). It becomes even shorter than the lambda if FooObject needs to be const. I use this technique a lot. For instance I can do: std::sort(people.begin(), people.end(), compare_by_member(&Person::last_name)) to sort a container of Persons by last_name. I've been doing that since long before C++11 and still prefer it over lambdas because it's shorter, easier to type, and clearer when read. Here's the code for compare_by_member if anyone's curious: https://gist.github.com/anonymous/5163229 https://gist.github.com/anonymous/5163229