c++ - std::function vs std::function& as a function argument -
this question has answer here:
consider 3 functions below,
func1(std::function<size_t(...) > f, ...); func2(std::function<size_t(...) >& f ); func3(const std::function<size_t(...)> &f);
for other type of argument passing value/copy-constructor, passing reference , passing const reference have clear context , use cases known.
for case of function<>
objects, e.g. passing const reference save time (from e.g. calling potential copy constructor) or space (no need pass whole function object stack)? how big function object in first place make worth passing passing const reference? guess would size of pointer - correct?
for case of
function<>
objects, e.g. passing const reference save time (from e.g. calling potential copy constructor) or space (no need pass whole function object stack)?
maybe. you'd have measure it.
how big function object in first place make worth passing passing const reference?
it depends on implementation. you'd have measure it.
my guess would size of pointer - correct?
implementations encouraged use "small object optimisation", small function object stored inside function
object, rather dynamically allocated. in case size of (or larger than) object. otherwise, size of (or larger than) pointer dynamic object.
Comments
Post a Comment