Reputation: 14603
My question refers to:
Using a lambda expression versus a private method
Now that lambda functors are part of C++, they could be used to unclutter a class' interface. How does lambda use vs private method use compare in C++? Do there exist better alternatives to unclutter class interfaces?
Upvotes: 7
Views: 3199
Reputation: 4076
How does lambda use vs private method use compare in C++
One may capture the this pointer, therefore having access to a class's internals without cluttering its interface. Previously, under certain circumstances one would require either a member or a friend function (use in conjunction with bind and mem_fn) in stl algorithms.
Added, one should strive to keep interfaces minimal, as changing them is more costly. For this reason idioms like pimpl are popular, and for the same reason a lambda may be preferred over a member function. As mentioned by another person, one should consider that functions are also tools of reuse, but in that case I would prefer a pimpl with private functions, which negates reuse of lambdas for this purpose.
Upvotes: 3
Reputation: 10863
Lambda is a way to move logic into the flow of your code. The only purpose is improving readability, and they can help or they can hurt
Upvotes: 3
Reputation: 726479
Although lambdas can definitely replace some private member functions, thinking of them as means to uncluttering the interface of a class is taking too narrow a view of both lambdas and private member functions.
Private member functions (and functions in general) are the basic units of code reuse. They let you write a piece of logic once, and then hide it behind the name of the function.
Although lambdas can replace a private member function in certain context, they could replace an object of which that function is a member along with it, which is a whole lot more. Due to lambda's ability to capture the context around them, you get a way of creating code blocks that take not only your object with it, but also the state of your local variables. Before lambdas you needed to create a special class for that; lambdas let you create such classes on the fly, for much better readability.
Upvotes: 5
Reputation: 1753
It has a matter of personal taste in it if you ask me, the difference between the two is not huge.
Lambdas are really good for stl algorithms, while private methods, are not as comftorable for that, on the other hand, i find private methods much better if you want to reuse your code.
This question shows the best usages of lambdas imo : lambdas
Personally, i prefer private methods, espessialy in the case you presented. Lambdas should not be used instead of private methods. It clutters the function's body and diverts the eye from the main logic.
Although it unclutter the class, it clutters the function, which i find worse.
Upvotes: 2
Reputation: 4084
Do there exist better alternatives to unclutter class interfaces?
A method I use quite often is to provide a series of functions with file-scope in an unnamed namespace that act as "helpers" to my member functions.
For example:
// Foobar.cpp
#include "Foobar.h"
namespace {
void someHelper()
{
std::cout << "I'm a helper!" << std::endl;
}
}
void Foobar::someMemberFunc()
{
someHelper();
[...]
}
You can, of course, also declare lambdas and/or classes in this unnamed namespace as well. Anything declared in this unnamed namespace will only be accessible from within the same translation unit (eg, cpp file).
Upvotes: 5