SlideShare una empresa de Scribd logo
1 de 33
Descargar para leer sin conexión
Andrey Upadyshev
Hot C++:

Rvalue References And
Move Semantics
Licensed under a CC BY-SA 4.0 License. Version of 2015.06.19
1
Overview
❖ Rvalue references!
❖ Move semantics!
❖ It’s well assisted!!
❖ More ways to shoot yourself in the foot
2
Rvalue References
3
Lvalue And Rvalue (how came from C)
• lvalue - may appear on the left hand side of an
assignment, represents storage region locator value, i.e.
evaluates to object identity.!
• All the rest is non-lvalue or rvalue (because can appear
on the right hand side of assignment only)
4
Lvalue And Rvalue, Example 1
int a = 42; // OK lvalue on left side of assignment!
int b = 43; // and rvalue on right side!
!
a = b; // OK, lvalue may be on any side of assignment!
b = a; // OK, lvalue may be on any side of assignment!
!
int c = a * b; // OK, rvalue on right side of assignment!
a * b = 42; // err, rvalue on left hand side of assignment
!
int *p = &i; // OK, i is an lvalue!
int *p1 = &43; // err, cannot take the address of an rvalue!
5
Value Categories (C++11)
• lvalue - is an expression that identifies a non-
temporary object!
• prvalue (pure rvalue) - is an expression that
identifies a temporary object or is a value not
associated with any object (C++03 rvalue)!
• Ex: The result of calling a function whose return
type is a value!
• xvalue - is an expression that identifies an "eXpiring"
object, that is, the object that may be moved from!
• Ex: The result of calling a function whose return
type is an rvalue reference
6
Simplified!
}rvalues
std::cout << &bar().member; // ?
Lvalue And Rvalue, Example 2
Foo& foo(); // `foo()` is lvalue

foo() = 42; // OK

std::cout << &foo(); // OK
Bar bar(); // `bar()` is prvalue

Bar && pub(); // `pub()` is xvalue

Bar b2 = bar(); // OK

bar() = b2; // cannot assign to an rvalue

std::cout << &bar(); // cannot take the address of an rvalue

std::cout << &pub(); // …
7
std::cout << &bar().member; // reference to a member of an rvalue

// is an rvalue!
Lvalue References
void foo(const Bar& bar); //[1]

Binds to lvalue and rvalue. Can not modify bar.!
void foo(Bar& bar); //[2]

Binds to lvalue only. Can modify bar. (Old Visual Studios binds
it to rvalue too. But this does not conform to the Standard)
8
Bar read_bar(const char *filename);
foo(read_bar(“bar.txt”)); ?
foo(read_bar(“bar.txt”)); // [1] => bar is const
Rvalue References
9
void foo(const Bar& bar); //[1]
Binds to lvalue and rvalue. Can not modify bar.!
void foo(Bar& bar); //[2]
Binds to lvalue only. Can modify bar.!
void foo(Bar && bar); //[3]
Binds to rvalue only. Takes precedence over lvalue overloads. Can
modify bar.!
void foo(const Bar && bar); //[4]
Binds to rvalue only. Takes precedence over lvalue overloads. Can not
modify bar. [Almost] has no useful meaning. Use const ref overload
instead.
foo(read_bar(“bar.txt”)); // [3] => bar is mutable!
Why Do I Need Them At All?
void foo(Bar && rv);!
❖ In short: to use move semantics!!
❖ Longer:!
❖ Because rvalue references bind to rvalue, they can be
used to extend the lifetime of a modifiable temporary
(an rvalue is a temporary, remember?).!
❖ You can do funny things with temporary, e.g. safely
steal any data you need from it (nobody cares, heh).
It’s called move semantics.
10
Move Semantics
11
Move Semantics In Example
Copying!
MemBuf lv = rv;
class MemBuf { void *m_data; size_t m_size; ... };
12
F1 23 4C DB 98 73 11 ...
rv
allocate
and copy
F1 23 4C DB 98 73 11 ...
lv contains a copy of rv's
content
lv
Moving!
MemBuf lv = std::move(rv);
F1 23 4C DB 98 73 11 ...
rv
lv
lv grabs (steals) content of rv
rv in moved-from state
X
Moved-From Objects
❖ Moving-from does not abolish destructing. The object’s destructor
is called anyway!!
❖ Moved-from object should be placed in a valid but unspecified
state, so it is safe to:!
❖ destroy it!
❖ assign one a new value (is assignable)!
❖ A type may provide more strong guaranty. E.g. STL’s smart
pointers guaranty that moved-from object is empty.
13
F1 23 4C DB 98 73 11 ...
rv
lv
X
Why Move Semantics?
The best possible performance for classes with expensive copy while
keeping the clear interface.!
❖ std::vector<HugeObject> performing bad in C++03:!
❖ Copies elements on insertion and even more on growing.!
❖ Can be passed around only wrapped with smart pointer.!
❖ So we used std::vector<std::shared_ptr<HugeObject>> or
boost::ptr_vector<HugeObject> to gain performance but got new problems:!
❖ Extra allocation and extra level of indirection (performance!)!
❖ Extra complexity!
❖ Doesn’t work smoothly with STL algorithms (OK, ptr_vector does)!
❖ Thanks to move semantics std::vector<HugeObject> become good in C++11
(finally!):!
❖ Elements are moved on insertion and on growing!
❖ No [smart] pointer involved overhead!
❖ Clear and simple, STL algorithms works perfect!
❖ Can be moved around
14
Why Move Semantics?
❖ Clearer interface for non-copyable but movable objects (like files,
threads etc):!
❖ std::auto_ptr<File> openFile(

const char *filename); // C++03!
❖ File openFile(const char *filename); // C++11
15
Adding Move Semantics To a Class
❖ There are two new special member functions:!
❖ Move constructor:

Foo::Foo(Foo && rv);!
❖ Move assignment operator:

Foo& operator= (Foo && rv);!
❖ Like copy ctor/assignment operator, but moves from its argument.
❖ The functions are compiler generated by default (use it!) but may be user
provided, explicitly defaulted or deleted.!
❖ Compiler uses similar rules as for copy ctor/copy assignment operator
generation.!
❖ Visual Studio before 2015 does not generate move ctor/move
assignment. You must do it manually.
16
Forcing Move Semantics
template<class T>

void swap_opt(T& a, T& b)

{

T tmp(a);

a = b;

b = tmp;

}!
Hm, copying…
std::move just casts lvalue to rvalue (no moving itself!) what allows a move semantics
to be used. Scott Meyers said that perhaps one should be called rvalue_cast
17
template<class T>

void swap_opt(T& a, T& b)

{ 

T tmp(std::move(a));

a = std::move(b);

b = std::move(tmp);

}!
Moving!!!
Let’s implement optimized swap that utilize move semantics.!
For simplicity, assume that it will be applied only to movable types.
Forcing move semantics leaves moved from objects behind, so must be uses carefully:!
std::string tmp("bla-bla");

std::string s(std::move(tmp));

std::cout << tmp; // Using of 'moved from' object (e.g. by mistake) for

// other then assigning. Undefined behavior for most types
It’s Well Assisted
18
Supported By STL
❖ C++11 STL is significantly extended to support rvalue references
and move semantics:!
❖ All containers and other types storing user types (pair, tuple,
future, function, smart pointers etc) are move-aware.!
❖ New algorithms: move, move_forward!
❖ New iterator adapter: move_iterator!
❖ Movable, non-copyable unique_ptr replaces flawy auto_ptr
(which is deprecated)!
❖ Optimized swap for movable types!
❖ New type traits
19
Ref Qualifiers
❖ Member functions can be specified with ref-qualifier & or &&, placed
after cv-qualifier:!
❖ const Foo& Bar::foo() const & { // [1]

return m_foo;

}
❖ Foo&& Bar::foo() && { // [2]

return std::move(m_foo); // `*this` is always

} // lvalue
❖ Allow to have different overloads for lvalue and rvalue (temporary).!
❖ No ref-qualifier means “whatever” (for backward compatibility)!
❖ A function with a ref-qualifier can not overload with a function without
one.
20
Perfect Forwarding & Forwarding
References
❖ Perfect forwarding is finally possible! !
❖ Thanks to forwarding (aka universal) references and new
reference collapsing rules:!
❖ template<class P1, class P2>

void create_foo(P1 && p1, P2 && p2) {

Foo foo(std::forward<P1>(p1),

std::forward<P2>(p2));

…

}
❖ Looks even better with variadic templates (not a sarcasm!)!
❖ It is a big topic for another meeting
21
Optimization
❖ Copy elision does elision of both copy and move
operations!
❖ When copy elision is not applicable, compiler prefers
moving over copying.
22
Special Member Functions
❖ Move constructor and move assignment operator aren’t generated if copy
constructor, copy assignment operator or destructor is explicitly declared.!
❖ Copy constructor and copy assignment operator aren't generated if move
constructor or move assignment operator is explicitly declared!
❖ Not supported before Visual Studio 2015!
❖ Rule of three become rule of five:

If a class defines one of the following it should probably explicitly define all five:!
•destructor!
•copy constructor!
•copy assignment operator!
•move constructor!
•move assignment operator
23
More Ways To Shoot Yourself In
The Foot
24
Named Rvalue Reference Is Not an Rvalue
Foo::Foo(Bar && bar)

: m_bar(bar) {} //[1] Is something wrong?
Named rvalue reference identifies an object => it is lvalue.

i.e. bar is copied unless move semantics is forced!
Foo::Foo(Bar && bar)

: m_bar(std::move(bar)) {} //[2] OK
Foo::Foo(Foo && rh)

: m_bar(rh.m_bar) {} //[3] copy, ref to lvalue member is an lvalue
Foo::Foo(Foo && rh)

: m_bar(std::move(rh.m_bar)) {} //[4] OK

// or

: m_bar(std::move(rh).m_bar) {} //[5] OK, ref to rvalue member is

// rvalue
25
Returning of Rvalue Reference
Foo && make_foo() { //[1] Is something wrong?

return Foo(42);

}
An rvalue reference is a reference. Returning a reference to a
local object is bad. Return by value instead!
Foo make_foo() { //[2] OK

return Foo(42);

}
Return rvalue reference only when you really know that you need this. A right
example is std::move:!
template<class T>

typename std::remove_reference<T>::type&& std::move(T && t) {

return static_cast<typename std::remove_reference<T>::type &&>(t);

}
26
❖ Applying std::move to a const object does
not activate move semantics! You quietly got
copy instead of move.!
❖ const Foo f = make_foo();

return Bar(std::move(f));

// Moving of `f` is blocked
❖ std::move does not remove object constness!
❖ No compilation error generated in such case.!
❖ Confusing unjustified behavior. One can write
something like safe_move to protect himself.
std::move And Const Objects
27
Move Semantics and RVO
28
struct Foo {…};
Foo make_foo1() {

Foo r(…);

return std::move(r);

}
Everything correct?!
RVO is blocked, r is moved or copied!
Foo make_foo1() {

Foo r(…);

return r;

}
RVO is applied, no move or copy
Move Semantics and RVO
29
struct Bar {…};
struct Foo {

Foo(const Bar& rh);

Foo(Bar && rh);

…

};
Foo make_foo2() {
Bar bar(…);
return bar;

}
Everything correct?!
Actually it is `return Foo(bar)`. bar is copied!
Foo make_foo2() {
Bar bar(…);
return std::move(bar);

}
Actually it is `return Foo(std::move(bar))`. bar is moved. We can’t get any better here
Move Semantics And Exception Safety
❖ // Huge object

struct Elephant {

Elephant(const Elephant& op);

Elephant(Elephant && op);

Elephant& operator= (const Elephant& op);

Elephant& operator= (Elephant && op);

…

};



std::vector<Elephant> all_elephants;

… // Heavily use it
❖ Does everything look good?!
30
Move Semantics And Exception Safety
For good reason STL containers may copy elements internally unless
ones' move constructor doesn't throw. Special traits are used:!
std::move_if_noexcept!
std::is_nothrow_move_constructible!
To get rid of copy, add noexcept to the move special functions (only
if function really doesn't throw, no cheating, please!):!
Elephant(Elephant && op) noexcept;
Note that noexcept is not supported in Visual Studio before 2015, use
macro like BOOST_NOEXCEPT (<boost/config/suffix.hpp>):!
Elephant(Elephant && op) BOOST_NOEXCEPT;
31
Useful Links
Alex Allain, Move semantics and rvalue references in C++11

http://www.cprogramming.com/c++11/rvalue-references-and-move-semantics-in-c++11.html!
Dave Abrahams, Exceptionally Moving!

http://web.archive.org/web/20130524084627/http://cpp-next.com/archive/2009/10/exceptionally-
moving/!
Stephan T. Lavavej, Don’t Help the Compiler

http://channel9.msdn.com/Events/GoingNative/2013/Don-t-Help-the-Compiler!
Andrzej Krzemieński, Ref-qualifiers

https://akrzemi1.wordpress.com/2014/06/02/ref-qualifiers/!
Max Galkin. C++ curiosities: one does not simply move a const object

http://yacoder.net/blog/2015/05/06/cpp-curiosities-one-does-not-simply-move-a-const-object/!
C++ Reference

http://en.cppreference.com/w/!
C++11 Standard (final plus minor editorial changes)!
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3337.pdf
32
Questions?
33
I HAVE NO IDEA

WHAT’S GOING ON.

Más contenido relacionado

La actualidad más candente

Regular types in C++
Regular types in C++Regular types in C++
Regular types in C++Ilio Catallo
 
Gentle introduction to modern C++
Gentle introduction to modern C++Gentle introduction to modern C++
Gentle introduction to modern C++Mihai Todor
 
C++ 11 Features
C++ 11 FeaturesC++ 11 Features
C++ 11 FeaturesJan Rüegg
 
Presentation 2nd
Presentation 2ndPresentation 2nd
Presentation 2ndConnex
 
06 -working_with_strings
06  -working_with_strings06  -working_with_strings
06 -working_with_stringsHector Garzo
 
C traps and pitfalls for C++ programmers
C traps and pitfalls for C++ programmersC traps and pitfalls for C++ programmers
C traps and pitfalls for C++ programmersRichard Thomson
 
C++11: Feel the New Language
C++11: Feel the New LanguageC++11: Feel the New Language
C++11: Feel the New Languagemspline
 
Introduction to c programming
Introduction to c programmingIntroduction to c programming
Introduction to c programmingSaranyaK68
 
GNAT Pro User Day: Ada 2012, Ravenscar and SPARK running on an Atmel ARM M4 (...
GNAT Pro User Day: Ada 2012, Ravenscar and SPARK running on an Atmel ARM M4 (...GNAT Pro User Day: Ada 2012, Ravenscar and SPARK running on an Atmel ARM M4 (...
GNAT Pro User Day: Ada 2012, Ravenscar and SPARK running on an Atmel ARM M4 (...AdaCore
 
Summary of effective modern c++ item1 2
Summary of effective modern c++ item1 2Summary of effective modern c++ item1 2
Summary of effective modern c++ item1 2Young Ha Kim
 
(5) cpp abstractions essential_operators
(5) cpp abstractions essential_operators(5) cpp abstractions essential_operators
(5) cpp abstractions essential_operatorsNico Ludwig
 

La actualidad más candente (20)

Regular types in C++
Regular types in C++Regular types in C++
Regular types in C++
 
C++11
C++11C++11
C++11
 
Gentle introduction to modern C++
Gentle introduction to modern C++Gentle introduction to modern C++
Gentle introduction to modern C++
 
C++ 11 Features
C++ 11 FeaturesC++ 11 Features
C++ 11 Features
 
Modern C++
Modern C++Modern C++
Modern C++
 
Smart Pointers in C++
Smart Pointers in C++Smart Pointers in C++
Smart Pointers in C++
 
Presentation 2nd
Presentation 2ndPresentation 2nd
Presentation 2nd
 
What's New in C++ 11?
What's New in C++ 11?What's New in C++ 11?
What's New in C++ 11?
 
Modern C++
Modern C++Modern C++
Modern C++
 
The Style of C++ 11
The Style of C++ 11The Style of C++ 11
The Style of C++ 11
 
06 -working_with_strings
06  -working_with_strings06  -working_with_strings
06 -working_with_strings
 
C traps and pitfalls for C++ programmers
C traps and pitfalls for C++ programmersC traps and pitfalls for C++ programmers
C traps and pitfalls for C++ programmers
 
C++11: Feel the New Language
C++11: Feel the New LanguageC++11: Feel the New Language
C++11: Feel the New Language
 
String notes
String notesString notes
String notes
 
Introduction to c programming
Introduction to c programmingIntroduction to c programming
Introduction to c programming
 
GNAT Pro User Day: Ada 2012, Ravenscar and SPARK running on an Atmel ARM M4 (...
GNAT Pro User Day: Ada 2012, Ravenscar and SPARK running on an Atmel ARM M4 (...GNAT Pro User Day: Ada 2012, Ravenscar and SPARK running on an Atmel ARM M4 (...
GNAT Pro User Day: Ada 2012, Ravenscar and SPARK running on an Atmel ARM M4 (...
 
Summary of effective modern c++ item1 2
Summary of effective modern c++ item1 2Summary of effective modern c++ item1 2
Summary of effective modern c++ item1 2
 
(5) cpp abstractions essential_operators
(5) cpp abstractions essential_operators(5) cpp abstractions essential_operators
(5) cpp abstractions essential_operators
 
Summary of C++17 features
Summary of C++17 featuresSummary of C++17 features
Summary of C++17 features
 
Strings
StringsStrings
Strings
 

Similar a [OLD VERSION, SEE DESCRIPTION FOR NEWER VERSION LINK] Hot C++: Rvalue References And Move Semantics

Elements of C++11
Elements of C++11Elements of C++11
Elements of C++11Uilian Ries
 
JavaScript Cheatsheets with easy way .pdf
JavaScript Cheatsheets with easy way .pdfJavaScript Cheatsheets with easy way .pdf
JavaScript Cheatsheets with easy way .pdfranjanadeore1
 
gdscWorkShopJavascriptintroductions.pptx
gdscWorkShopJavascriptintroductions.pptxgdscWorkShopJavascriptintroductions.pptx
gdscWorkShopJavascriptintroductions.pptxsandeshshahapur
 
Unit 3-Javascript.pptx
Unit 3-Javascript.pptxUnit 3-Javascript.pptx
Unit 3-Javascript.pptxAmanJha533833
 
Memory Management with Java and C++
Memory Management with Java and C++Memory Management with Java and C++
Memory Management with Java and C++Mohammad Shaker
 
1669958779195.pdf
1669958779195.pdf1669958779195.pdf
1669958779195.pdfvenud11
 
ARC - Moqod mobile talks meetup
ARC - Moqod mobile talks meetupARC - Moqod mobile talks meetup
ARC - Moqod mobile talks meetupMoqod
 
Compilation and Execution
Compilation and ExecutionCompilation and Execution
Compilation and ExecutionChong-Kuan Chen
 
Zero downtime deploys for Rails apps
Zero downtime deploys for Rails appsZero downtime deploys for Rails apps
Zero downtime deploys for Rails appspedrobelo
 
RubyConf Portugal 2014 - Why ruby must go!
RubyConf Portugal 2014 - Why ruby must go!RubyConf Portugal 2014 - Why ruby must go!
RubyConf Portugal 2014 - Why ruby must go!Gautam Rege
 
Introduction to Rust Programming Language
Introduction to Rust Programming LanguageIntroduction to Rust Programming Language
Introduction to Rust Programming LanguageJohn Liu
 
Esoft Metro Campus - Programming with C++
Esoft Metro Campus - Programming with C++Esoft Metro Campus - Programming with C++
Esoft Metro Campus - Programming with C++Rasan Samarasinghe
 
The C++ rvalue lifetime disaster. Arno Schödl ➠ CoreHard Autumn 2019
The C++ rvalue lifetime disaster. Arno Schödl ➠ CoreHard Autumn 2019The C++ rvalue lifetime disaster. Arno Schödl ➠ CoreHard Autumn 2019
The C++ rvalue lifetime disaster. Arno Schödl ➠ CoreHard Autumn 2019corehard_by
 

Similar a [OLD VERSION, SEE DESCRIPTION FOR NEWER VERSION LINK] Hot C++: Rvalue References And Move Semantics (20)

Elements of C++11
Elements of C++11Elements of C++11
Elements of C++11
 
C++ references
C++ referencesC++ references
C++ references
 
JavaScript Cheatsheets with easy way .pdf
JavaScript Cheatsheets with easy way .pdfJavaScript Cheatsheets with easy way .pdf
JavaScript Cheatsheets with easy way .pdf
 
Introduction to php basics
Introduction to php   basicsIntroduction to php   basics
Introduction to php basics
 
gdscWorkShopJavascriptintroductions.pptx
gdscWorkShopJavascriptintroductions.pptxgdscWorkShopJavascriptintroductions.pptx
gdscWorkShopJavascriptintroductions.pptx
 
Unit 3-Javascript.pptx
Unit 3-Javascript.pptxUnit 3-Javascript.pptx
Unit 3-Javascript.pptx
 
Memory Management with Java and C++
Memory Management with Java and C++Memory Management with Java and C++
Memory Management with Java and C++
 
C++11 move semantics
C++11 move semanticsC++11 move semantics
C++11 move semantics
 
1669958779195.pdf
1669958779195.pdf1669958779195.pdf
1669958779195.pdf
 
ARC - Moqod mobile talks meetup
ARC - Moqod mobile talks meetupARC - Moqod mobile talks meetup
ARC - Moqod mobile talks meetup
 
Compilation and Execution
Compilation and ExecutionCompilation and Execution
Compilation and Execution
 
Zero downtime deploys for Rails apps
Zero downtime deploys for Rails appsZero downtime deploys for Rails apps
Zero downtime deploys for Rails apps
 
RubyConf Portugal 2014 - Why ruby must go!
RubyConf Portugal 2014 - Why ruby must go!RubyConf Portugal 2014 - Why ruby must go!
RubyConf Portugal 2014 - Why ruby must go!
 
Unit 2.5
Unit 2.5Unit 2.5
Unit 2.5
 
Java script basics
Java script basicsJava script basics
Java script basics
 
Linkers And Loaders
Linkers And LoadersLinkers And Loaders
Linkers And Loaders
 
Introduction to Rust Programming Language
Introduction to Rust Programming LanguageIntroduction to Rust Programming Language
Introduction to Rust Programming Language
 
Esoft Metro Campus - Programming with C++
Esoft Metro Campus - Programming with C++Esoft Metro Campus - Programming with C++
Esoft Metro Campus - Programming with C++
 
Unit 2.5
Unit 2.5Unit 2.5
Unit 2.5
 
The C++ rvalue lifetime disaster. Arno Schödl ➠ CoreHard Autumn 2019
The C++ rvalue lifetime disaster. Arno Schödl ➠ CoreHard Autumn 2019The C++ rvalue lifetime disaster. Arno Schödl ➠ CoreHard Autumn 2019
The C++ rvalue lifetime disaster. Arno Schödl ➠ CoreHard Autumn 2019
 

Último

What Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationWhat Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationJuha-Pekka Tolvanen
 
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...WSO2
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...masabamasaba
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park masabamasaba
 
WSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaSWSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaSWSO2
 
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...masabamasaba
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfonteinmasabamasaba
 
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...WSO2
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...masabamasaba
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...Jittipong Loespradit
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnAmarnathKambale
 
WSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - KeynoteWSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - KeynoteWSO2
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park masabamasaba
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024VictoriaMetrics
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisamasabamasaba
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...SelfMade bd
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastPapp Krisztián
 
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2
 
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benonimasabamasaba
 

Último (20)

What Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationWhat Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the Situation
 
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
WSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaSWSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaS
 
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
WSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - KeynoteWSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - Keynote
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the past
 
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
 
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
 

[OLD VERSION, SEE DESCRIPTION FOR NEWER VERSION LINK] Hot C++: Rvalue References And Move Semantics

  • 1. Andrey Upadyshev Hot C++:
 Rvalue References And Move Semantics Licensed under a CC BY-SA 4.0 License. Version of 2015.06.19 1
  • 2. Overview ❖ Rvalue references! ❖ Move semantics! ❖ It’s well assisted!! ❖ More ways to shoot yourself in the foot 2
  • 4. Lvalue And Rvalue (how came from C) • lvalue - may appear on the left hand side of an assignment, represents storage region locator value, i.e. evaluates to object identity.! • All the rest is non-lvalue or rvalue (because can appear on the right hand side of assignment only) 4
  • 5. Lvalue And Rvalue, Example 1 int a = 42; // OK lvalue on left side of assignment! int b = 43; // and rvalue on right side! ! a = b; // OK, lvalue may be on any side of assignment! b = a; // OK, lvalue may be on any side of assignment! ! int c = a * b; // OK, rvalue on right side of assignment! a * b = 42; // err, rvalue on left hand side of assignment ! int *p = &i; // OK, i is an lvalue! int *p1 = &43; // err, cannot take the address of an rvalue! 5
  • 6. Value Categories (C++11) • lvalue - is an expression that identifies a non- temporary object! • prvalue (pure rvalue) - is an expression that identifies a temporary object or is a value not associated with any object (C++03 rvalue)! • Ex: The result of calling a function whose return type is a value! • xvalue - is an expression that identifies an "eXpiring" object, that is, the object that may be moved from! • Ex: The result of calling a function whose return type is an rvalue reference 6 Simplified! }rvalues
  • 7. std::cout << &bar().member; // ? Lvalue And Rvalue, Example 2 Foo& foo(); // `foo()` is lvalue
 foo() = 42; // OK
 std::cout << &foo(); // OK Bar bar(); // `bar()` is prvalue
 Bar && pub(); // `pub()` is xvalue
 Bar b2 = bar(); // OK
 bar() = b2; // cannot assign to an rvalue
 std::cout << &bar(); // cannot take the address of an rvalue
 std::cout << &pub(); // … 7 std::cout << &bar().member; // reference to a member of an rvalue
 // is an rvalue!
  • 8. Lvalue References void foo(const Bar& bar); //[1]
 Binds to lvalue and rvalue. Can not modify bar.! void foo(Bar& bar); //[2]
 Binds to lvalue only. Can modify bar. (Old Visual Studios binds it to rvalue too. But this does not conform to the Standard) 8 Bar read_bar(const char *filename); foo(read_bar(“bar.txt”)); ? foo(read_bar(“bar.txt”)); // [1] => bar is const
  • 9. Rvalue References 9 void foo(const Bar& bar); //[1] Binds to lvalue and rvalue. Can not modify bar.! void foo(Bar& bar); //[2] Binds to lvalue only. Can modify bar.! void foo(Bar && bar); //[3] Binds to rvalue only. Takes precedence over lvalue overloads. Can modify bar.! void foo(const Bar && bar); //[4] Binds to rvalue only. Takes precedence over lvalue overloads. Can not modify bar. [Almost] has no useful meaning. Use const ref overload instead. foo(read_bar(“bar.txt”)); // [3] => bar is mutable!
  • 10. Why Do I Need Them At All? void foo(Bar && rv);! ❖ In short: to use move semantics!! ❖ Longer:! ❖ Because rvalue references bind to rvalue, they can be used to extend the lifetime of a modifiable temporary (an rvalue is a temporary, remember?).! ❖ You can do funny things with temporary, e.g. safely steal any data you need from it (nobody cares, heh). It’s called move semantics. 10
  • 12. Move Semantics In Example Copying! MemBuf lv = rv; class MemBuf { void *m_data; size_t m_size; ... }; 12 F1 23 4C DB 98 73 11 ... rv allocate and copy F1 23 4C DB 98 73 11 ... lv contains a copy of rv's content lv Moving! MemBuf lv = std::move(rv); F1 23 4C DB 98 73 11 ... rv lv lv grabs (steals) content of rv rv in moved-from state X
  • 13. Moved-From Objects ❖ Moving-from does not abolish destructing. The object’s destructor is called anyway!! ❖ Moved-from object should be placed in a valid but unspecified state, so it is safe to:! ❖ destroy it! ❖ assign one a new value (is assignable)! ❖ A type may provide more strong guaranty. E.g. STL’s smart pointers guaranty that moved-from object is empty. 13 F1 23 4C DB 98 73 11 ... rv lv X
  • 14. Why Move Semantics? The best possible performance for classes with expensive copy while keeping the clear interface.! ❖ std::vector<HugeObject> performing bad in C++03:! ❖ Copies elements on insertion and even more on growing.! ❖ Can be passed around only wrapped with smart pointer.! ❖ So we used std::vector<std::shared_ptr<HugeObject>> or boost::ptr_vector<HugeObject> to gain performance but got new problems:! ❖ Extra allocation and extra level of indirection (performance!)! ❖ Extra complexity! ❖ Doesn’t work smoothly with STL algorithms (OK, ptr_vector does)! ❖ Thanks to move semantics std::vector<HugeObject> become good in C++11 (finally!):! ❖ Elements are moved on insertion and on growing! ❖ No [smart] pointer involved overhead! ❖ Clear and simple, STL algorithms works perfect! ❖ Can be moved around 14
  • 15. Why Move Semantics? ❖ Clearer interface for non-copyable but movable objects (like files, threads etc):! ❖ std::auto_ptr<File> openFile(
 const char *filename); // C++03! ❖ File openFile(const char *filename); // C++11 15
  • 16. Adding Move Semantics To a Class ❖ There are two new special member functions:! ❖ Move constructor:
 Foo::Foo(Foo && rv);! ❖ Move assignment operator:
 Foo& operator= (Foo && rv);! ❖ Like copy ctor/assignment operator, but moves from its argument. ❖ The functions are compiler generated by default (use it!) but may be user provided, explicitly defaulted or deleted.! ❖ Compiler uses similar rules as for copy ctor/copy assignment operator generation.! ❖ Visual Studio before 2015 does not generate move ctor/move assignment. You must do it manually. 16
  • 17. Forcing Move Semantics template<class T>
 void swap_opt(T& a, T& b)
 {
 T tmp(a);
 a = b;
 b = tmp;
 }! Hm, copying… std::move just casts lvalue to rvalue (no moving itself!) what allows a move semantics to be used. Scott Meyers said that perhaps one should be called rvalue_cast 17 template<class T>
 void swap_opt(T& a, T& b)
 { 
 T tmp(std::move(a));
 a = std::move(b);
 b = std::move(tmp);
 }! Moving!!! Let’s implement optimized swap that utilize move semantics.! For simplicity, assume that it will be applied only to movable types. Forcing move semantics leaves moved from objects behind, so must be uses carefully:! std::string tmp("bla-bla");
 std::string s(std::move(tmp));
 std::cout << tmp; // Using of 'moved from' object (e.g. by mistake) for
 // other then assigning. Undefined behavior for most types
  • 19. Supported By STL ❖ C++11 STL is significantly extended to support rvalue references and move semantics:! ❖ All containers and other types storing user types (pair, tuple, future, function, smart pointers etc) are move-aware.! ❖ New algorithms: move, move_forward! ❖ New iterator adapter: move_iterator! ❖ Movable, non-copyable unique_ptr replaces flawy auto_ptr (which is deprecated)! ❖ Optimized swap for movable types! ❖ New type traits 19
  • 20. Ref Qualifiers ❖ Member functions can be specified with ref-qualifier & or &&, placed after cv-qualifier:! ❖ const Foo& Bar::foo() const & { // [1]
 return m_foo;
 } ❖ Foo&& Bar::foo() && { // [2]
 return std::move(m_foo); // `*this` is always
 } // lvalue ❖ Allow to have different overloads for lvalue and rvalue (temporary).! ❖ No ref-qualifier means “whatever” (for backward compatibility)! ❖ A function with a ref-qualifier can not overload with a function without one. 20
  • 21. Perfect Forwarding & Forwarding References ❖ Perfect forwarding is finally possible! ! ❖ Thanks to forwarding (aka universal) references and new reference collapsing rules:! ❖ template<class P1, class P2>
 void create_foo(P1 && p1, P2 && p2) {
 Foo foo(std::forward<P1>(p1),
 std::forward<P2>(p2));
 …
 } ❖ Looks even better with variadic templates (not a sarcasm!)! ❖ It is a big topic for another meeting 21
  • 22. Optimization ❖ Copy elision does elision of both copy and move operations! ❖ When copy elision is not applicable, compiler prefers moving over copying. 22
  • 23. Special Member Functions ❖ Move constructor and move assignment operator aren’t generated if copy constructor, copy assignment operator or destructor is explicitly declared.! ❖ Copy constructor and copy assignment operator aren't generated if move constructor or move assignment operator is explicitly declared! ❖ Not supported before Visual Studio 2015! ❖ Rule of three become rule of five:
 If a class defines one of the following it should probably explicitly define all five:! •destructor! •copy constructor! •copy assignment operator! •move constructor! •move assignment operator 23
  • 24. More Ways To Shoot Yourself In The Foot 24
  • 25. Named Rvalue Reference Is Not an Rvalue Foo::Foo(Bar && bar)
 : m_bar(bar) {} //[1] Is something wrong? Named rvalue reference identifies an object => it is lvalue.
 i.e. bar is copied unless move semantics is forced! Foo::Foo(Bar && bar)
 : m_bar(std::move(bar)) {} //[2] OK Foo::Foo(Foo && rh)
 : m_bar(rh.m_bar) {} //[3] copy, ref to lvalue member is an lvalue Foo::Foo(Foo && rh)
 : m_bar(std::move(rh.m_bar)) {} //[4] OK
 // or
 : m_bar(std::move(rh).m_bar) {} //[5] OK, ref to rvalue member is
 // rvalue 25
  • 26. Returning of Rvalue Reference Foo && make_foo() { //[1] Is something wrong?
 return Foo(42);
 } An rvalue reference is a reference. Returning a reference to a local object is bad. Return by value instead! Foo make_foo() { //[2] OK
 return Foo(42);
 } Return rvalue reference only when you really know that you need this. A right example is std::move:! template<class T>
 typename std::remove_reference<T>::type&& std::move(T && t) {
 return static_cast<typename std::remove_reference<T>::type &&>(t);
 } 26
  • 27. ❖ Applying std::move to a const object does not activate move semantics! You quietly got copy instead of move.! ❖ const Foo f = make_foo();
 return Bar(std::move(f));
 // Moving of `f` is blocked ❖ std::move does not remove object constness! ❖ No compilation error generated in such case.! ❖ Confusing unjustified behavior. One can write something like safe_move to protect himself. std::move And Const Objects 27
  • 28. Move Semantics and RVO 28 struct Foo {…}; Foo make_foo1() {
 Foo r(…);
 return std::move(r);
 } Everything correct?! RVO is blocked, r is moved or copied! Foo make_foo1() {
 Foo r(…);
 return r;
 } RVO is applied, no move or copy
  • 29. Move Semantics and RVO 29 struct Bar {…}; struct Foo {
 Foo(const Bar& rh);
 Foo(Bar && rh);
 …
 }; Foo make_foo2() { Bar bar(…); return bar;
 } Everything correct?! Actually it is `return Foo(bar)`. bar is copied! Foo make_foo2() { Bar bar(…); return std::move(bar);
 } Actually it is `return Foo(std::move(bar))`. bar is moved. We can’t get any better here
  • 30. Move Semantics And Exception Safety ❖ // Huge object
 struct Elephant {
 Elephant(const Elephant& op);
 Elephant(Elephant && op);
 Elephant& operator= (const Elephant& op);
 Elephant& operator= (Elephant && op);
 …
 };
 
 std::vector<Elephant> all_elephants;
 … // Heavily use it ❖ Does everything look good?! 30
  • 31. Move Semantics And Exception Safety For good reason STL containers may copy elements internally unless ones' move constructor doesn't throw. Special traits are used:! std::move_if_noexcept! std::is_nothrow_move_constructible! To get rid of copy, add noexcept to the move special functions (only if function really doesn't throw, no cheating, please!):! Elephant(Elephant && op) noexcept; Note that noexcept is not supported in Visual Studio before 2015, use macro like BOOST_NOEXCEPT (<boost/config/suffix.hpp>):! Elephant(Elephant && op) BOOST_NOEXCEPT; 31
  • 32. Useful Links Alex Allain, Move semantics and rvalue references in C++11
 http://www.cprogramming.com/c++11/rvalue-references-and-move-semantics-in-c++11.html! Dave Abrahams, Exceptionally Moving!
 http://web.archive.org/web/20130524084627/http://cpp-next.com/archive/2009/10/exceptionally- moving/! Stephan T. Lavavej, Don’t Help the Compiler
 http://channel9.msdn.com/Events/GoingNative/2013/Don-t-Help-the-Compiler! Andrzej Krzemieński, Ref-qualifiers
 https://akrzemi1.wordpress.com/2014/06/02/ref-qualifiers/! Max Galkin. C++ curiosities: one does not simply move a const object
 http://yacoder.net/blog/2015/05/06/cpp-curiosities-one-does-not-simply-move-a-const-object/! C++ Reference
 http://en.cppreference.com/w/! C++11 Standard (final plus minor editorial changes)! http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3337.pdf 32
  • 33. Questions? 33 I HAVE NO IDEA
 WHAT’S GOING ON.

Notas del editor

  1. Greeting to Ray, listeners friends and colleagues Who I'm , What I’m, Notify about first time and language - Notify about regalement - Notify to write slide number to refer in questions - Very quickly about first and second topics in general and whom it addressed to
  2. more details about presentation itself and whom it addressed to
  3. How many value categories do you know in C++11? 0? 2? more?
  4. QUIZ, is it compiles OK?
  5. QUIZ: 1 or 2 will be called?
  6. QUIZ, something wrong?
  7. Who know what is ref qualifier?
  8. Who knows, what is a universal reference?
  9. QUIZ: What is wrong? How to fix this?
  10. Is something wrong?
  11. Who knows what RVO means? Who knows what NRVO means?