dimanche 1 juillet 2018

C++: 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.

I'm targeting C++11.

Aucun commentaire:

Enregistrer un commentaire