Search code examples
c++return-value-optimization

Likeliness of Named RVO?


I have a function that looks like this:

// Fetch 1 MB of data
void GetData(std::vector<char> & outData);

The 1MB is exaggerated, but I just want to make the point that it's preferable to avoid unnecessary copies.

If I add this overload:

std::vector<char> GetData()
{
    std::vector<char> result;
    GetData(result);
    return result;
}

Then how likely is it that RVO will kick in?


Solution

  • With most reasonably recent compilers (e.g., VS 2005 or newer, gcc 3.4 or newer), it's essentially certain. I only say "most" because I haven't tested every compiler in existence. Every new compiler I've looked at in probably the last 5 years or so has included it.