
2008/11/6 Mathias Gaunard <mathias.gaunard@ens-lyon.org>:
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.
Thank you for the information! After having found all three kinds of functor passing in boost libraries and the information from Steve Watanabe that there is no official boost policy for that I tend to choose method (2): Passing by const-reference: * It is in line with the standard method of passing objects e.g. [Meyers 98, item 22]. * It supports the claim to design functors as pure functions, the result of which only depends on their arguments (and at most a constant state that was created on construction) [Meyers 01, item 39] [Sutter,Alexandescu 05, item 87]. So the disadvantages of const-reference passing (operator()(..) is const and the functor's state is const) appear to me as a guard against misuse. In addition unintentional copying of fat functors can not happen. So I am happy with that :) Cheers Joachim