Posts

Showing posts with the label function-object

When using templates to support functor as arguments, what qualifier should I use?

When using templates to support functor as arguments, what qualifier should I use? Consider this code: template<class F> void foo1(F f) { f(); } template<class F> void foo2(F const& f) { f(); } template<class F> void foo3(F&& f) { f(); } Which version of foo should I use? foo1 is what I see most "in the wild" but I fear that it might introduce copies that I don't want. I have a custom functor that is kind of heavy to copy so I would like to avoid that. Currently I'm leaning towards foo3 (as foo2 would disallow mutating functors) but I'm unsure about the implications. foo foo1 foo3 foo2 I'm targeting C++11. foo3 receive a forwarding reference but does not forward. The body should be std::forward<F>(f)() . I'd still suggest foo1 though. – Guillaume Racicot Jul 1 at 14:44 ...