c++ - std::function vs std::function& as a function argument -


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

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -