C++ references vs return values

前端 未结 4 1884
天涯浪人
天涯浪人 2021-01-12 15:34

I understand the principle point of references is to avoid making copies of large structures, but what if a function you\'re writing itself creates a large structure? Is it

4条回答
  •  情话喂你
    2021-01-12 15:48

    My advice is to write the code so that the client usage is as intuitive as possible.

    If - as is usually the case - you judge that to be returning by value, then if your system is performance critical and you find a particular return value is not being optimised nicely by your compiler - even at the highest reasonable optimisation level you can use and after checking for any relevant command line options or compiler tips on achieving RVO, and considering alternative compilers if practical - ideally a C++11 compiler that benefits from always-efficient move semantics, only then change the specific problematic spot to accept the return value by reference.

提交回复
热议问题