Questions tagged [nrvo]

C++ copy-elision of named (thus non-temporary) return-values

(Named) Return Value Optimization is an exception to the as-if rule governing optimizations C++ implementations may perform.

It allows elliding a copy of the (named) return-value, if it is a local variable or a temporary.

See:

83 questions
21
votes
1 answer

Avoid const locals that are returned?

I always thought that it's good to have const locals be const void f() { const resource_ptr p = get(); // ... } However last week I watched students that worked on a C++ exercise and that wondered about a const pointer being…
Johannes Schaub - litb
  • 496,577
  • 130
  • 894
  • 1,212
15
votes
1 answer

Named Return Value Optimization when using std::optional

I recently discovered std::optional as a way to improve the clarity of my code, especially for return value of functions. However I had questions about its impact on performance. More specifically I wanted to know if it was possible to write a code…
Romain D.
  • 310
  • 1
  • 2
  • 10
12
votes
1 answer

Can a C++ compiler perform RVO for a const return value?

Let's say I have the function #include std::string const foo() { std::string s = "bar"; return s; } int main() { std::string t = foo(); } Can a compiler perform (named) return-value optimization for t, even though the types…
user541686
  • 205,094
  • 128
  • 528
  • 886
11
votes
1 answer

Are compilers clever enough to std::move variables going out of scope?

Consider the following piece of code: std::vector Foo() { std::vector v = Bar(); return v; } return v is O(1), since NRVO will omit the copy, constructing v directly in the storage where the function's return value would otherwise…
André Harder
  • 354
  • 2
  • 11
11
votes
1 answer

Shouldn't NRVO guarantee the local named variable and the call-site variable to take the same address?

I think it should, because it's important for correctness. However, I'm surprised to see Clang's output. Consider the code below: #include struct S { int i; S(int i) : i(i) {} S(S&&) { std::cout << "S(S&&)\n"; …
Jamboree
  • 5,139
  • 2
  • 16
  • 36
9
votes
1 answer

How does C++ ABI deal with RVO and NRVO?

I am confused with regards how do compiler and linker deal with the fact that requirements on the caller of the function differ depending on if the function uses RVO or NRVO. This could be my misunderstanding, but my assumption is that generally…
NoSenseEtAl
  • 28,205
  • 28
  • 128
  • 277
9
votes
4 answers

Will RVO happen when returning std::pair?

A function needs to return two values to the caller. What is the best way to implement? Option 1: pair myfunc() { ... return make_pair(getU(),getV()); } pair mypair = myfunc(); Option 1.1: // Same defn U u; V v; tie(u,v) =…
balki
  • 26,394
  • 30
  • 105
  • 151
8
votes
2 answers

Why is clang not optimizing this with NRVO?

I'm trying to reason why a reasonably good C++ 11 compiler (clang) is not optimizing this code, and wondering if anybody here has opinions. #include #define SLOW struct A { A() {} ~A() { std::cout << "A d'tor\n"; } A(const A&) {…
dmaclach
  • 3,403
  • 1
  • 21
  • 23
8
votes
2 answers

Why NRVO is not applied here?

NRVO is not applied when I run this code in VS2010. #include class A { public: A() { printf( "I am in constructor\n" ); } A(const A& a) { printf( "I am in copy constructor\n" ); } ~A() { printf( "I am in destructor\n" );…
Belloc
  • 6,318
  • 3
  • 22
  • 52
7
votes
2 answers

Is RVO applied on this situation?

Let's say we have this situation std::string v_1() { return "name"; } std::string test = v_1(); Is RVO applied here? I think that the answer is no, because one the rules to apply RVO is: "If a function returns a class type by value, and the…
user2296145
  • 276
  • 2
  • 8
6
votes
2 answers

Can a C++ compiler perform RVO for a named const variable used for the return value?

This question is a slight variant on a related question shown here. In C++17 I have a local variable that I want to be const to demonstrate it is unmodified once created per Scott Meyers Effective C++ item 3 recommendation to use const whenever…
WilliamKF
  • 41,123
  • 68
  • 193
  • 295
6
votes
3 answers

C++ nrvo/copy elision with return statement in parentheses

i was fooling around with the following code and got different results using my visual studio 2017 application and two different online compilers. in release mode visual studio does elide the copy/move in both cases, while the two online compilers…
phön
  • 1,215
  • 8
  • 20
6
votes
1 answer

Complete example of (N)RVO

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... …
emesx
  • 12,555
  • 10
  • 58
  • 91
5
votes
6 answers

Return Value Optimization - C++ - Destructor calls

The following code calls the destructor 4 times: #include using namespace std; class A{ public: A(){cout<<"A"<
Venky
  • 71
  • 4
5
votes
2 answers

Can NRVO be turned off in debug builds?

In the program as follows #include struct A { A() { std::cout << "0"; } A( const A & ) { std::cout << "1"; } A( A && ) noexcept { std::cout << "2"; } }; A foo() { A res; return res; } int main() { const A a =…
Fedor
  • 17,146
  • 13
  • 40
  • 131
1
2 3 4 5 6