Why should I ever return something by value, since C++ features const references?

前端 未结 3 1288
忘掉有多难
忘掉有多难 2021-02-12 15:34

Consider this function:

Thing func(){
    return something;
}

Every call to this function, a copy of something is made and passed

3条回答
  •  遥遥无期
    2021-02-12 16:15

    If you're returning a non-static local variable, then you must return by value, since it will be destroyed when the function returns, making any references to it invalid. In that case, the copy can often be replaced by a move, or elided altogether.

    If you're returning something persistent, then you're right; it's generally better to return a reference and let the caller decide whether it should be copied. But there might be other reasons such as thread safety to prefer returning by value even then: by returning a copy, the reader can't access the persistent object while another thread might be modifying it.

提交回复
热议问题