Avoiding copy of objects with the “return” statement

前端 未结 7 2214
慢半拍i
慢半拍i 2020-11-27 16:16

I have a very basic question in C++. How to avoid copy when returning an object ?

Here is an example :

std::vector test(const uns         


        
7条回答
  •  没有蜡笔的小新
    2020-11-27 16:40

    This program can take advantage of named return value optimization (NRVO). See here: http://en.wikipedia.org/wiki/Copy_elision

    In C++11 there are move constructors and assignment which are also cheap. You can read a tutorial here: http://thbecker.net/articles/rvalue_references/section_01.html

提交回复
热议问题