Complete example of (N)RVO

451 views Asked by At

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.
1

There are 1 answers

3
Benjamin Lindley On BEST ANSWER

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.