But in that case, since almost all of them are pure functors (don't keep a
state), I think using values is more efficient than keeping pointers to
instances, since it will allow the compiler to create more optimisations on
them (and again, this only holds on stateless functors).

On Wed, Jan 28, 2015 at 1:29 AM, Benjamin Mahler <[email protected]>
wrote:

> It's not something that is done consistently, but we've often used the ()
> operator for things that feel like a function, and consequently may be
> replaced with functions or lambdas as we move more towards C++11. Keep in
> mind that the vast majority of our C++ was written before we had any C++11
> support whatsoever. :)
>
> For example, if you want to filter a collection, you would want to pass a
> functor predicate as opposed to a subclass of a Predicate type:
>
> filtered = filter(ints, [](int i) { return i > 5; });
>
> vs.
>
> template <typename T>
> class GreaterThanFive : public Predicate
> {
>   bool apply(const T& t) { return t > 5; }
> };
>
> filtered = filter(ints, predicate);
>
> Here the type system isn't really adding much value, what we really care
> about is the *signature* of the predicate.
>
> On Tue, Jan 27, 2015 at 1:56 AM, Alexander Rojas <[email protected]>
> wrote:
>
> > I was checking master.cpp, and I saw that a lot of the objects there are
> > functors, which is OK unless you get a pointer to the functor instance
> and
> > then call its operator() which looks something like
> > (*instance)(parameters). I have always find this syntax somewhat
> confusing
> > since it looks a lot like an old C cast.
> >
> > Is there a rationale for using this functor model everywhere?
> >
> > Isn’t it possible to use the operator as a wrapper for some method call,
> > so when applied on a pointer we call the method directly? I find that
> much
> > more readable.
>

Reply via email to