To get a trial key
fill out the form below
Team License (a basic version)
Enterprise License (an extended version)
* By clicking this button you agree to our Privacy Policy statement

Request our prices
New License
License Renewal
--Select currency--
USD
EUR
GBP
RUB
* By clicking this button you agree to our Privacy Policy statement

Free PVS-Studio license for Microsoft MVP specialists
* By clicking this button you agree to our Privacy Policy statement

To get the licence for your open-source project, please fill out this form
* By clicking this button you agree to our Privacy Policy statement

I am interested to try it on the platforms:
* By clicking this button you agree to our Privacy Policy statement

Message submitted.

Your message has been sent. We will email you at


If you haven't received our response, please do the following:
check your Spam/Junk folder and click the "Not Spam" button for our message.
This way, you won't miss messages from our team in the future.

>
>
>
RVO and NRVO

RVO and NRVO

Sep 17 2021

RVO (Return Value Optimization) is a compiler optimization. In some cases, it allows not to create a local object that is used as a return value.

Instead, the returned object is constructed in place of the function call. This eliminates the unnecessary move/copy constructor call.

Look at the example:

std::vector<int> GetVector()
{
  return std::vector<int>(1'000'000, 1);
}

void foo()
{
  auto vect = GetVector();
}

Here the value returned by the GetVector function is immediately created in memory allocated for the vect object. At the same time, the move/copy constructor is eliminated.

We may apply RVO if the object returned from a function is a prvalue expression. This expression must have the same type as the type of the function's return value by signature, without cv qualifiers. Besides, with C++17, RVO is no longer an optimization, but a rule that compilers must follow. This rule is applied even if a move/copy constructor has side effects.

The RVO rule is one of the compiler rules that implement the temporary materialization mechanics. It's a principle, according to which a prvalue expression isn't physically created in memory until it's assigned to a non-prvalue object. Such compiler behavior reduces the number of copies. As a result, it speeds up the program execution.

There's another optimization type — NRVO (Named Return Value Optimization). This compiler optimization type is like RVO. Instead of creating a local return object and then moving/copying it in place of the function call, this optimization instantly creates it in the right place. Its difference from RVO is that NRVO is applied to lvalue objects.

For example, in the following code fragment, instead of RVO, we apply NRVO to the returned result object:

std::vector<int> GetVector2()
{
  std::vector<int> result(1'000'000, 1);
  return result;
}

void foo()
{
  auto vect = GetVector();
  ....
}

NRVO occurs in the following way. The compiler pre-allocates and initializes the object which is supposed to receive the result of a function. Then the function gets a pointer to this object. This pointer is used in the function body instead of the actual object the function returns.

Effectively, NRVO transforms the code fragment above into the following:

void GetVector2(std::vector<int> *x)
{
  new (x) std::vector<int>(1'000'000, 0);
}

void foo()
{
  auto *x = static_cast<std::vector<int> *>(
              alloca(sizeof(std::vector<int>)));
  GetVector2(x);
  ....
  delete x;
}

However, we can apply NRVO only when the type of the actually returned object and the type of the object returned according to the function signature completely coincide.

There is also an anti-pattern that disables the use of NRVO:

typename <typename Res, typename ...T>
Res foo(T ...)
{
  Res result;
  // some calculations
  return std::move(result);
}

void bar()
{
  auto obj = foo<SomeObject>();
}

Here we should remove the std::move call. Code tries to 'tell' the compiler that the returned object must be moved with std::move and not copied. Despite that, the compiler is obliged to generate slower assembly code.

That's because the returned object is the result of calling the std::move function and its type is Res &&. The actually returned type and the type function return type according to its signature are different. Therefore, the compiler cannot apply NRVO for the foo function. We are dealing not with an optimization, but with a pessimization.

The C++11 standard says that if a compiler cannot apply an optional optimization, it must do the following. First, it must apply the move constructor. Then apply the copy constructor for local variables or formal function parameters.

Popular related articles
The Ultimate Question of Programming, Refactoring, and Everything

Date: Apr 14 2016

Author: Andrey Karpov

Yes, you've guessed correctly - the answer is "42". In this article you will find 42 recommendations about coding in C++ that can help a programmer avoid a lot of errors, save time and effort. The au…
Free PVS-Studio for those who develops open source projects

Date: Dec 22 2018

Author: Andrey Karpov

On the New 2019 year's eve, a PVS-Studio team decided to make a nice gift for all contributors of open-source projects hosted on GitHub, GitLab or Bitbucket. They are given free usage of PVS-Studio s…
Static analysis as part of the development process in Unreal Engine

Date: Jun 27 2017

Author: Andrey Karpov

Unreal Engine continues to develop as new code is added and previously written code is changed. What is the inevitable consequence of ongoing development in a project? The emergence of new bugs in th…
How PVS-Studio Proved to Be More Attentive Than Three and a Half Programmers

Date: Oct 22 2018

Author: Andrey Karpov

Just like other static analyzers, PVS-Studio often produces false positives. What you are about to read is a short story where I'll tell you how PVS-Studio proved, just one more time, to be more atte…
Appreciate Static Code Analysis!

Date: Oct 16 2017

Author: Andrey Karpov

I am really astonished by the capabilities of static code analysis even though I am one of the developers of PVS-Studio analyzer myself. The tool surprised me the other day as it turned out to be sma…
Characteristics of PVS-Studio Analyzer by the Example of EFL Core Libraries, 10-15% of False Positives

Date: Jul 31 2017

Author: Andrey Karpov

After I wrote quite a big article about the analysis of the Tizen OS code, I received a large number of questions concerning the percentage of false positives and the density of errors (how many erro…
PVS-Studio ROI

Date: Jan 30 2019

Author: Andrey Karpov

Occasionally, we're asked a question, what monetary value the company will receive from using PVS-Studio. We decided to draw up a response in the form of an article and provide tables, which will sho…
Technologies used in the PVS-Studio code analyzer for finding bugs and potential vulnerabilities

Date: Nov 21 2018

Author: Andrey Karpov

A brief description of technologies used in the PVS-Studio tool, which let us effectively detect a large number of error patterns and potential vulnerabilities. The article describes the implementati…
The Evil within the Comparison Functions

Date: May 19 2017

Author: Andrey Karpov

Perhaps, readers remember my article titled "Last line effect". It describes a pattern I've once noticed: in most cases programmers make an error in the last line of similar text blocks. Now I want t…
The Last Line Effect

Date: May 31 2014

Author: Andrey Karpov

I have studied many errors caused by the use of the Copy-Paste method, and can assure you that programmers most often tend to make mistakes in the last fragment of a homogeneous code block. I have ne…

Comments (0)

Next comments
This website uses cookies and other technology to provide you a more personalized experience. By continuing the view of our web-pages you accept the terms of using these files. If you don't want your personal data to be processed, please, leave this site.
Learn More →
Accept