Efficient use of move semantics together with (N)RVO

后端 未结 3 1233
梦谈多话
梦谈多话 2021-02-03 14:53

Let\'s say I want to implement a function that is supposed to process an object and return a new (possibly changed) object. I would like to do this as efficient as possible in C

3条回答
  •  独厮守ぢ
    2021-02-03 15:32

    The fastest way to do this is- if the argument is lvalue, then copy it and return that copy- if rvalue, then move it. The return can always be moved or have RVO/NRVO applied. This is easily accomplished.

    Object process1(Object arg) {
        return std::move(arg.makeChanges());
    }
    

    This is very similar to the canonical C++11 forms of many kinds of operator overloads.

提交回复
热议问题