Boost logo

Boost :

Subject: Re: [boost] Design conventions; passing functors
From: Mathias Gaunard (mathias.gaunard_at_[hidden])
Date: 2008-11-06 11:58:31

Joachim Faulhaber wrpte:

> Now, there seems to be a convention from the STL that functors
> shall be passed BY VALUE.
> [...]
> I do not really understand this convention and feel some
> resistance to follow it.

That convention only exists because of the forwarding problem.
If you pass by const-reference, the object becomes const, the state must
then be mutable and the operator() const.
If you pass by reference, you cannot take temporaries.
If you pass by value, you have no problem, except that you're working
with a copy, and thus you should return it so that the state changes can
be accessed. The copy may also be a useless cost.

The forwarding problem being solved by rvalue references, you may use
that if available (boost has a macro).

> In addition the call by value
> implementation can (and will) lead to heavy inefficiency
> by unaware usage of fat functors. So I browsed through
> some boost libraries and found that functors are passed by
> reference there (e.g. accumulators, fusion).

lambda used that approach to forward arguments, you had to use
make_const to wrap temporaries.
I'm not sure how phoenix does it (it doesn't seem to be documented), but
I assume it does the same.

Boost list run by bdawes at, gregod at, cpdaniel at, john at