Memory location of return values of functions, relating to functional programming

Dear experts

I sometimes wonder how Memory location of return values of functions is handled. This question is mainly because some techniques in functional programming (std::function & std::bind, and lambda) entails chains of function calls. As you know, if argument type of functions has large data size, it makes overhead due to copy operations. Therefore, It is desirable to use reference argument. However, I wonder if return values in chains of function calls are available for such a reference argument (only pass by value)?
After all, I am worried about whether huge overhead occurs when size of return value is large (c.f. stl containers etc).

If you are familiar with such a problem,
I would appreciate it if you could advise me.
(Is there technique to avoid such a overhead?)

Kind regards
I am worried about whether huge overhead occurs when size of return value is large
Typically there is no such problem, because in most cases, the compiler will place a function's return value exactly where it is required, without incurring any unavoidable copies.

This particular behavior is a type of copy elision, in particular return value optimization and named return value optimization:
https://en.cppreference.com/w/cpp/language/copy_elision

Compare this with the principle behind std::vector::emplace_back.
Last edited on
Dear mbozzi

I am going to use functional programming techniques in terms of copy elision,
thank you so much for your appropriate answer.

Kind regards

Topic archived. No new replies allowed.