Named return value elision

suggest change

If you return an lvalue expression from a function, and this lvalue:

If all of these are the case, then the copy/move from the lvalue can be elided:

std::string func()
{
  std::string str("foo");
  //Do stuff
  return str;
}

More complex cases are eligible for elision, but the more complex the case, the less likely the compiler will be to actually elide it:

std::string func()
{
  std::string ret("foo");
  if(some_condition)
  {
    return "bar";
  }
  return ret;
}

The compiler could still elide ret, but the chances of them doing so go down.

As noted earlier, elision is not permitted for value parameters.

std::string func(std::string str)
{
  str.assign("foo");
  //Do stuff
  return str; //No elision possible
}

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


Copy elision:
* Named return value elision

Table Of Contents
8 Arrays
11 Loops
39 Streams
51 Unions
56 Lambdas
60 SFINAE
62 RAII
67 Sorting
75 Copy elision
84 RTTI
87 Scopes
104 Profiling
107 Recursion
117 Iteration
125 Alignment
134 Semaphore
136 Debugging
139 Mutexes
142 decltype