mercredi 29 avril 2015

std::function vs std::function& as a function argument

consider the three functions below,

func1(std::function<size_t(...) > f, ...);
func2(std::function<size_t(...) >& f );
func3(const std::function<size_t(...)> &f);  

For any other type of argument passing by value/copy-constructor, passing by reference and passing by const reference have a clear context and their use cases are well known.

For the case of function<> objects, would e.g. passing by const reference save time (from e.g. calling potential copy constructor) or space (no need to pass a whole function object to the stack)? How big is a function object in the first place to make it worth passing passing by const reference? my guess would be that it would roughly be the size of a pointer - is this correct?

Aucun commentaire:

Enregistrer un commentaire