Hot C++11, part 1: Rvalue References And Move Semantics
Upcoming SlideShare
Loading in...5
×
 

Hot C++11, part 1: Rvalue References And Move Semantics

on

  • 1,051 views

The slides for my mini-lecture "Hot C++11: Rvalue References And Move Semantics" that I've done for my colleagues in the company I'm working at. Note that the slides without the lecture itself can be ...

The slides for my mini-lecture "Hot C++11: Rvalue References And Move Semantics" that I've done for my colleagues in the company I'm working at. Note that the slides without the lecture itself can be not clear enough.

Statistics

Views

Total Views
1,051
Views on SlideShare
1,045
Embed Views
6

Actions

Likes
1
Downloads
15
Comments
0

1 Embed 6

https://twitter.com 6

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

CC Attribution-ShareAlike LicenseCC Attribution-ShareAlike License

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Hot C++11, part 1: Rvalue References And Move Semantics Hot C++11, part 1: Rvalue References And Move Semantics Presentation Transcript

  • Andrey Upadyshev Hot C++11:
 Rvalue References And Move Semantics Licensed under a CC BY-SA 4.0 License. Version of 2014.08.19
  • Lvalue And Rvalue (how comes from C) • lvalue - may appear on the left or on the right hand side of an assignment! • rvalue - can only appear on the right hand side of an assignment 2
  • 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; // error, rvalue on left hand side of assignment 3 View slide
  • Lvalue And Rvalue (C++ style) • lvalue - locator value: refers to a defined region of storage (allow to take address via the & operator)! • rvalue - is an expression that is not an lvalue! • rvalue - is expression resulting to temporary object 4 View slide
  • Lvalue And Rvalue, Example 2 int i = 42; int *p = &i; // ok, i is an lvalue int *p1 = &43; // error, cannot take the address of an rvalue ! int& foo() { ... } foo() = 42; // ok, foo() is an lvalue int *p2 = &foo(); // ok, foo() is an lvalue ! int bar() { ... } i = bar(); // ok, foobar() is an rvalue bar() = 42; // error, cannot assign to an rvalue int *p3 = &bar(); // error, cannot take the address of an rvalue 5
  • Rvalue Reference void foo(const Bar& bar); ! Can not modify bar. Accepts lvalue and rvalue.! void foo(Bar& bar); ! Can modify bar. Accepts lvalue only (Visual Studio also accepts rvalue. But this does not conform the Standard)! void foo(Bar&& bar); ! Can modify bar. Accepts rvalue only. Takes precedence over Bar& and const Bar&.! void foo(const Bar&& bar); ! Allowed but has no useful meaning. Use const Bar& instead. 6
  • Move Semantics In Example class BigArray { void *m_data; ... }; BigArray& BigArray::operator= (const BigArray& rhs) { // set m_data into copy of memory that rhs.m_data points to } BigArray& BigArray::operator= (BigArray&& rhs) { // swap m_data and rhs.m_data (i.e. two pointers) } Similar reasoning applies to the copy constructor and move constructor. 7
  • Why Move Semantics? Useful for:! • Classes with expensive copy (std::vector)! • Non-copyable classes (class File)! • Better performance and clear interfaces:! void insert(const string& s); // copying t void insert(string&& s); // moving s (no copy) std::auto_ptr<File> openFile(filename); File openFile(filename); 8
  • Forcing Move Semantics template<class T> void swap(T& a, T& b) { T tmp(a); a = b; b = tmp; } ! X a, b; swap(a, b); ! template<class T> void swap(T& a, T& b) { T tmp(std::move(a)); a = std::move(b); b = std::move(tmp); } C++98 C++11 std::move does not move but casts lvalue to rvalue
 so move semantics forced 9
  • Rvalue Reference Is Not Rvalue void foo(Bar&& bar) { Bar bar2 = bar; // Copy. 'bar' is a named var so it's lvalue not rvalue } ! void foo(Bar&& bar) { Bar bar2 = std::move(bar); // Move (move semantics forced) } ! class Foo { std::vector<int> m_vec; std::list<double> m_list; ... Foo(Foo&& op) : m_vec(std::move(op.m_vec)) , m_list(std::move(op.m_list)) {} }; 10
  • Move Semantics and RVO Foo bar() { Foo res; ... return std::move(res); // Bad, no RVO } ! Foo bar() { Foo res; ... return res; // Perfect } ! Foo bar() { Bar tmp; // It's Bar, not Foo! ... return std::move(tmp); // Good. Actually it will be Foo(std::move(tmp)) } 11
  • Move Semantics And Exception Safety For safety reasons STL containers can copy unless element's move constructor does not throw exceptions.! std::move_if_noexcept std::is_nothrow_move_constructible! Foo::Foo(Foo&& op) noexcept; Not supported in Visual Studio before 2014 → use macro like ! BOOST_NOEXCEPT_OR_NOTHROW (boost/config/suffix.hpp):! Foo::Foo(Foo&& op) BOOST_NOEXCEPT_OR_NOTHROW; 12
  • Compiler-Generated Functions • Compiler can generate move constructor and move assignment operator! • Not generated if explicitly declared either one of:! • destructor! • copy constructor! • copy assignment operator! • move constructor! • move assignment operator! • Change from C++98: copy constructor and copy assignment operator aren't generated if move constructor or move assignment operator is explicitly declared! • Not supported before Visual Studio 2013 rule of five 13 }
  • 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/! Move constructors
 http://en.cppreference.com/w/cpp/language/move_constructor! C++11 Standard (final plus minor editorial changes)! http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3337.pdf 14