c++ - How to avoid the copy when I return -


i have function returns vector or set:

set<int> foo() {     set<int> bar;     // create , massage bar     return bar; }  set<int> afoo = foo(); 

in case, create temporary memory space in function foo(), , assign afoo copying. want avoid copy, easy way can in c++11? think has rvalue thing.

ok, update question: if going return object defined myself, not vector or set thing, mean should define move constructor? this:

class value_to_return {   value_to_return (value_to_return && other) {     // how write here? think std::move supposed used?   } } 

thanks!!!

modem c++ compiler implement: given type t:

  • if t has accessible copy or move constructor, compiler may choose elide copy. so-called (named) return value optimization (rvo), specified before c++11 , supported compilers.
  • otherwise, if t has move constructor, t moved(since c++11).
  • otherwise, if t has copy constructor, t copied.
  • otherwise, compile-time error emitted.

Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

.htaccess - Matching full URL in RewriteCond -