RVO

G++ compiler will apply return value optimization(RVO), it prevent return value to have any copy or move constructor.

#include <iostream>

struct C {
  C() {}
  C(const C&) { std::cout << "A copy was made.\n"; }
};

C f() {
  return C();
}

int main() {
  std::cout << "Hello World!\n";
  C obj = f();
  return 0;
}

Depending on compiler, it may have three results

Hello World!
A copy was made.
A copy was made.
Hello World!
A copy was made.
Hello World!

But RVO has some limitation, see IBM blog

Reference

  1. RVO wiki
  2. IBM blog

results matching ""

    No results matching ""