6

I've been reading about (N)RVO and would like one, complete scenario description. I hope this question will serve other C++ -learners to clarify their ideas.

Suppose this scenario:

string get_string() {
    string x("racecar");
    //work on x...
    return x;
}

string a( get_string() );
string b = get_string();

Please disregard the C++11 move-semantics for a moment.

  • If no (N)RVO is performed, how many constructors/assignments/destructors will be executed? (please point out, to which objects to they refer)
  • What changes if (N)RVO is applied?
  • Finally, how does the situation change in C++11 assuming, that std::string supports move-semantics.
emesx
  • 12,555
  • 10
  • 58
  • 91

1 Answers1

6

1) Inside get_string, one string object (x) will be constructed using the constructor which takes a const char*.

2) When the function returns, the string constructed inside will be copy constructed to a temporary string object in the caller's space.

3) The temporary will be copy constructed to a.

4) See 1

5) See 2

6) See 3, but the copy will go to b

With RVO, 2 an 5 can be eliminated by constructing the temporary inside the function via an invisible reference. With further copy elision (not RVO), 3 and 6 can be eliminated. So that leaves us with 2 constructions, both using the const char* constructor.

With C++11 move semantics, the situation doesn't change at all if the compiler was good enough to get all the copy elision done. If copy elision isn't done, then 2, 3, 5 and 6 still exist, but become moves instead of copies. Unlike copy elision though, these moves are not an optional optimization. A conforming compiler must perform them, assuming it didn't already perform copy elision.

Benjamin Lindley
  • 101,917
  • 9
  • 204
  • 274
  • 1
    RVO just elminiates copies (2) and (5). Copies (3) and (6) can be eliminated by the normal elision of temporaries independently of (n)RVO being applied. – CB Bailey Oct 20 '12 at 10:20
  • @CharlesBailey: You're right. I tend to conflate RVO to include general copy elision because I learned about both at the same time. – Benjamin Lindley Oct 20 '12 at 10:28
  • This is pretty much what I needed to know, thanks :) Do you have any links to topics you (perhaps) didn't mention in your answer? – emesx Oct 20 '12 at 12:33