Prev: Can there be parallel initialization of global variables in C++0x?
Next: [C++03] Temporaries passed to constructor yield invalid member references
From: Mark Zaytsev on 29 May 2010 08:39 On May 28, 4:05 am, Jesse Perla <jessepe...(a)gmail.com> wrote: > I have some functors that have more data that I might wish and > algorithms that execute them (e.g. some weird multidimensional > interpolators, etc.). I wrote some algorithms that accept these by > constant reference, with the assumption that the function supplies a > const operator(). i.e. stuff like: > template<typename F> double f(const F& f) > { > return f(.1); > > }; > > I realize that the std algorithms always pass functors by value. What > is the reasoning for this with stateless algorithms like find > predicates, transform, etc. (I can see why std::generate might want a > copy by value)? Is it just to allow the functors to have a non-const > operator()? Are there any other reasons, such as lack of inlining > with modern compilers, that I can't pass const&? > When you pass function you a. pass pointer to standalone function -- no need to create excessive ref b. pass functor, which is usually stateless -- no need to create ref to 0-sized object bottom line -- it more efficient to pass function by value -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |