Questions tagged [rvo]

C++ copy-elision of 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 .

143 questions
2
votes
1 answer

When the move constructor is actually called if we have (N)RVO?

I have understood from several questions here on SO that (N)RVO prevents the move constructor from being called, when an object is returned by value. Classic example: struct Foo { Foo() { std::cout << "Constructed\n"; } Foo(const Foo…
Ignorant
  • 2,411
  • 4
  • 31
  • 48
2
votes
1 answer

RVO: Return value passed by value even if explicitly assigned to a const reference

I have a settings framework, which eventually caches values storing them into an std::map of boost::any. Since I don't want the client to deal with exceptions, it provides a default value the settings framework would fallback in case the setting…
codeJack
  • 2,423
  • 5
  • 24
  • 31
2
votes
2 answers

Why is the move constructor being called?

Given my understanding of return value optimization, I am a confused as to why the move constructor is being called in the sample code below: #include #include class MyCustomType { public: MyCustomType() { std::cout…
Harry
  • 1,362
  • 12
  • 19
2
votes
1 answer

C++ RVO: when it happens?

http://coliru.stacked-crooked.com/a/c795a5d2bb91ae32 #include struct X { X(const char *) { std::cout << 1; } X(const X &) { std::cout << 2; } X(X &&) { std::cout << 3; } }; X f(X a) { return a; } X g(const char * b) { …
q0987
  • 34,938
  • 69
  • 242
  • 387
2
votes
1 answer

How does shared_ptr increase counter when passed by value?

I have this sample code below. I know little bit about RVO (return value optimization) and how copy constructor and assignment operator are skipped during the optimization and return of the value is placed directly on the memory on the left. So if…
solti
  • 4,339
  • 3
  • 31
  • 51
2
votes
1 answer

Force use of copy constructor / Avoid use of copy constructor

I'm currently writing a logging class (just for practice) and ran into an issue. I have two classes: The class Buffer acts as a temporary buffer and flushes itself in it's destructor. And the class Proxy that returns a Buffer instance, so I don't…
Schore
  • 885
  • 6
  • 16
2
votes
1 answer

Is a rvalue reference parameter that is returned by value an xvalue?

My understanding is that, in the following function, the expression foo in the statement return foo; is an xvalue, because the object it denotes is expiring (even though foo is an lvalue in previous statements): Foo bar() { Foo foo; …
ricab
  • 2,697
  • 4
  • 23
  • 28
2
votes
1 answer

Why is a public copy-constructor required when assigning the temporary return object to a reference?

Consider the following code: class MyClass { MyClass() { x = 0; } MyClass(const MyClass&) { x = 1; } public: int x; MyClass(MyClass&&) { x = 2; } static const MyClass f() { …
Veggie
  • 397
  • 2
  • 14
2
votes
2 answers

I'm returning non-named object from a function. Why RVO still kicks in?

Regarding this: Why does std::move prevent RVO? someone wrote that: "Hence in a return statement copy elision can only occur, if the expression is the name of a local variable" However I made a little test with GCC: class X { public: X() { …
rubix_addict
  • 1,811
  • 13
  • 27
2
votes
3 answers

C++11 expensive rvalue temporary

I've got some light objects to push around and manipulate, which I then like to include in a more complex one. There's a lookup table which should remain unmodified. The idea appears simple enough, but the one line doing this - b += c(a); - creates…
Meeuwisse
  • 157
  • 6
2
votes
2 answers

Why doesn't RVO happen for assignment operator? (C++)

Example: A myfunction() { return A(); } A a = myfunction(); // default ctor only (return value optimization) a = myfunction(); // default ctor and operator= Why can't the compiler just write the new object into the existing object? I believe all…
rmp251
  • 5,018
  • 4
  • 34
  • 46
2
votes
1 answer

Converting Constructor As The Return From A Function

Is it possible to use the converting constructor rather than copy constructor to return object from a function even if there is no RVO evolved(Suppose that the compiler is not supporting any such kind of optimizations)? The point of the question is…
1
vote
2 answers

Why g++ does not enable RVO here?

Consider that TEST code: #include using namespace std; class Klass { public: Klass() { cout << "Klass()" << endl; } Klass(const Klass& right) { cout << "Klass(const Klass& right)" << endl; } }; Klass create(Klass…
yves Baumes
  • 8,836
  • 7
  • 45
  • 74
1
vote
0 answers

Differences between return value optimization and `std::move`?

What are the differences between return value optimization and std::move? Do they depend on same internal implementation? I understand that there are many places where must use std::move. std::move and RVO both could achieve copy elision.For…
sunshilong369
  • 646
  • 1
  • 5
  • 17
1
vote
0 answers

Why is my function that returns by value slower than function that using pass_by_reference?

I understand that c++ core guidelines specify that std::vector should be returned by value (in order for RVO/NRVO/move semantics to take place) as opposed to a pass by reference operation. When I tested this however with the below benchmark code it…