C++ references vs return values

前端 未结 4 1883
天涯浪人
天涯浪人 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 16:04

    Well, in your specific case (filling a container with values) I would go for an input iterator approach. But in all other cases (like a complicated string generation or some calculations) I wouldn't care about possible performance hits and trust RVO (see Jim's answer) and the compiler to optimize it like it should. If it becomes a bottleneck (profile!), change it of course!

提交回复
热议问题