3 ms·
Yeah 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
by agwa 14y ago
Yeah 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