0
C++ 2011                             Highlights from the New International Standard                                       ...
Intro                   •         Who am I                   •         Who are you?                   •         Why We’re ...
C++11 Goals                   •         Make C++ a better language for systems                             programming    ...
C++11 At a Glance                   •         General Core Language Features                             concurrency, move...
Ponies for Everybody!                             Big wins from little features                                           ...
Type Deduction                 void print_3x(vector<float> const& v)                 {                     for ( vector<fl...
Type Deduction                 void print_3x(vector<float> const& v)                 {                     for ( vector<fl...
Type Deduction                 void print_3x(vector<float> const& v)                 {                     for ( auto p = ...
Type Deduction                 void print_3x(vector<float> const& v)                 {                     for ( auto p = ...
Type Deduction              auto                 a = 3;               int              const auto           b = 3.14;     ...
Type Deduction                 void print_3x(vector<float> const& v)                 {                     for ( auto p = ...
Range-based for loop                 void print_3x(vector<float> const& v)                 {                     for ( aut...
Range-based for loop                 void print_3x(vector<float> const& v)                 {                     for ( flo...
Range-based for loop                 void print_3x(vector<float> const& v)                                           Range...
Range-based for loop                 void print_3x(vector<float> const& v)                    Loop variable          Range...
Range-based for loop                        auto in C++11                 void print_3x(vector<float> const& v)           ...
Range-based for loop                 template <class Range>                 void print_3x(Range const& v)                 ...
Range-based for loop                        auto in C++11                 template <class Range>                 void prin...
Range-based for loop                        auto in C++11                 void inplace_3x(vector<float>& v)               ...
Ranges Everywhere                             int rng[3] = { 42, 314, 77 };                             for( int var: rng ...
Ranges Everywhere                             std::pair<Iter,Iter> rng = … ;                             for( int var: rng...
Ranges Everywhere                             YourType rng;                             for( int var: rng )               ...
98     ++Range-based for loop    C                 void print_3x(vector<float> const& v)                 {                ...
98     ++Range-based“for”loop    C                 void print_3x(vector<float> const& v)                 {                ...
98     ++Range-based“for”loop    C                 void print_3x(vector<float> const& v)                 {                ...
Using an Algorithm                 void print_3x(vector<float> const& v)                 {                     std::for_ea...
Using an Algorithm                 void print_3x(vector<float> const& v)                 {                     std::for_ea...
Using an Algorithm                 void print_3x(vector<float> const& v)                 {                     std::for_ea...
Using an Algorithm                 static void print_3x_element(float a)                 {                     std::cout <...
Using an Algorithm                 static void print_3x_element(float a)                 {                     std::cout <...
Using an Algorithm                 static void print_3x_element(float a)                 {                     std::cout <...
Lambda Expressions                 void print_3x(vector<float> const& v)                 {                     std::for_ea...
Lambda Expressions                 void print_3x(vector<float> const& v)                 {                     std::for_ea...
Lambda Expressions                 void print_3x(vector<float> const& v)                 {                     std::for_ea...
Lambda Expressions                 void print_3x(vector<float> const& v)                 {                     std::for_ea...
What It Is                 void print_3x(vector<float> const& v)                 {                     std::for_each(     ...
anonymous class                                 What It Is                 struct __lambda143                 {           ...
anonymous class                                 What It Is                 struct __lambda143                 {           ...
anonymous class                                 What It Is                 struct __lambda143                 {           ...
Closing Angle Brackets                             std::vector<std::pair<int, std::string>> x;                            ...
Closing Angle Brackets                                                 Error in C++98: >>                                 ...
Closing Angle Brackets                             std::vector<std::pair<int, std::string>> x;                            ...
Move Semantics                                A Really Cheap Lunch                                         26Monday, Septe...
Copy Fatigue?                             typedef set<string> record;                             typedef vector<record> d...
Copy Fatigue?                             typedef set<string> record;                             typedef vector<record> d...
Copy Fatigue?                             typedef set<string> record;                             typedef vector<record> d...
Copy Fatigue?                             typedef set<string> record;                             typedef vector<record> d...
Copy Fatigue?                             typedef set<string> record;                             typedef vector<record> d...
Copy Fatigue?                             typedef set<string> record;                             typedef vector<record> d...
Copy Fatigue?                             typedef set<string> record;                             typedef vector<record> d...
Copy Fatigue?                             typedef set<string> record;                             typedef vector<record> d...
Copy Fatigue?                             typedef set<string> record;                             typedef vector<record> d...
Copy Fatigue?                             typedef set<string> record;                             typedef vector<record> d...
Copy Fatigue?                             typedef set<string> record;                             typedef vector<record> d...
Copy Fatigue?                             typedef set<string> record;                             typedef vector<record> d...
Copy Fatigue?                             typedef set<string> record;                             typedef vector<record> d...
Copy Fatigue?                             typedef set<string> record;                             typedef vector<record> d...
Copy Fatigue?                             typedef set<string> record;                             typedef vector<record> d...
Copy Fatigue?                             typedef set<string> record;                             typedef vector<record> d...
Copy Fatigue?                             typedef set<string> record;                             typedef vector<record> d...
Copy Fatigue?                             typedef set<string> record;                             typedef vector<record> d...
Try Moving Instead!                             typedef set<string> record;                             typedef vector<rec...
Try Moving Instead!                             typedef set<string> record;                             typedef vector<rec...
Try Moving Instead!                             typedef set<string> record;                             typedef vector<rec...
Try Moving Instead!                             typedef set<string> record;                             typedef vector<rec...
Try Moving Instead!                             typedef set<string> record;                             typedef vector<rec...
Try Moving Instead!                             typedef set<string> record;                             typedef vector<rec...
Try Moving Instead!                             typedef set<string> record;                             typedef vector<rec...
Try Moving Instead!                             typedef set<string> record;                             typedef vector<rec...
Try Moving Instead!                             typedef set<string> record;                             typedef vector<rec...
Try Moving Instead!                             typedef set<string> record;                             typedef vector<rec...
Move Mini-HOWTO                             Move-enabling an existing class                                           31Mo...
Move-Enabling                 struct Owner {                                         •   X                     Owner(const...
Move-Enabling                 struct Owner {                                         •   X                     Owner(const...
Move-Enabling                 struct Owner {                                         •   X                     Owner(const...
Move-Enabling                 struct Owner {                                         •   X                     Owner(const...
Move-Enabling                 struct Owner {                                         •   X                     Owner(const...
Move-Enabling                 struct Owner {                                         •   X                     Owner(const...
Move-Enabling                 struct Owner {                     Owner(const Owner& src)                       : p(src.p ?...
Move-Enabling                 struct Owner {                     Owner(const Owner& src)                       : p(src.p ?...
Move-Enabling                 struct Owner {                     Owner(const Owner& src)                       : p(src.p ?...
Move-Enabling                 struct Owner {                     Owner(const Owner& src)                       : p(src.p ?...
Move-Enabling                 struct Owner {                     Owner(const Owner& src)                       : p(src.p ?...
Move-Enabling                 struct Owner {                     Owner(const Owner& src)                       : p(src.p ?...
Move-Enabling                 struct Owner {                     Owner(const Owner& src)                       : p(src.p ?...
Move-Enabling                 struct Owner {                     Owner(const Owner& src)                       : p(src.p ?...
New Move Operations                 struct Owner {                     Owner(const Owner& src)                       : p(s...
New Move Operations                 struct Owner {                     Owner(const Owner& src)                            ...
New Move Operations                 struct Owner {                     Owner(const Owner& src)                     Owner& ...
Move Constructor                 struct Owner {                     Owner(const Owner& src);                     Owner& op...
Move Constructor                 struct Owner {                     Owner(const Owner& src);                     Owner& op...
Move Constructor                 struct Owner {                     Owner(const Owner& src);                     Owner& op...
Move Constructor                 struct Owner {                     Owner(const Owner& src);                     Owner& op...
Move Constructor                 struct Owner {                     Owner(const Owner& src);                     Owner& op...
Move Constructor                 struct Owner {                     Owner(const Owner& src);                     Owner& op...
Move Constructor                 struct Owner {                     Owner(const Owner& src);                     Owner& op...
Move Constructor                 struct Owner {                     Owner(const Owner& src);                     Owner& op...
Move Constructor                 struct Owner {                     Owner(const Owner& src);                     Owner& op...
Move Assignment                 struct Owner {                     Owner(const Owner& src);                     Owner& ope...
Move Assignment                 struct Owner {                     Owner(const Owner& src);                     Owner& ope...
Move Assignment                 struct Owner {                     Owner(const Owner& src);                     Owner& ope...
Move Assignment                 struct Owner {                     Owner(const Owner& src);                     Owner& ope...
Move Assignment                 struct Owner {                     Owner(const Owner& src);                     Owner& ope...
Move Assignment                 struct Owner {                     Owner(const Owner& src);                     Owner& ope...
Move Assignment                 struct Owner {                     Owner(const Owner& src);                     Owner& ope...
Move Semantics                   •         Automatically accelerates existing code!                   •         Applied th...
Containers                   •         Move semantics                   •         Hash containers                   •     ...
Other Useful Libraries                   •         regex:                   •         Algorithms                   •      ...
Smart Pointers                   •         unique_ptr                   •         shared_ptr                              ...
Classes and OOP                   •         member initializers                   •         explict conversion            ...
Concurrency                   •         async, futures, promise                   •         locks, mutexes and condition v...
Templates                   •         Variadics                   •         static_assert                   •         loca...
Metaprogramming                   •         constexpr                   •         Template aliases                   •    ...
What’s Next?                   •         Modules                   •         Concepts                   •         Pythy Sy...
Upcoming SlideShare
Loading in...5
×

C++11 (formerly known as C++0x) is the new C++ language standard. Dave Abrahams, BoostPro Computing

12,871

Published on

Dave Abrahams, BoostPro Computing

He is a founding member of Boost.org and an active participant in the ISO C++ standards committee. His broad range of experience in the computer industry includes shrink-wrap software development, embedded systems design and natural language processing. He has authored eight Boost libraries and has made contributions to numerous others. Dave made his mark on C++ standardization by developing a conceptual framework for understanding exception-safety and applying it to the C++ standard library. He created the first exception-safe standard library implementation and, with Greg Colvin, drafted the proposals that eventually became the standard library’s exception safety guarantees.

Presentation topic:
C++11 (formerly known as C++0x) is the new C++ language standard. Dave Abrahams, BoostPro Computing.

Key points:
The ISO C++ standardization committee has just unanimously approved its final draft international standard, and it's chock full of new features. Though a few of the features have been available for years, some are brand new, and nobody really knows what it's like to program in this new C++ language. As with C++03, Boost.org is expected to take a leading role in exploiting C++11. In this talk, I'll give an overview of the most important new developments.

Published in: Technology, Business
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
12,871
On Slideshare
0
From Embeds
0
Number of Embeds
9
Actions
Shares
0
Downloads
77
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Transcript of "C++11 (formerly known as C++0x) is the new C++ language standard. Dave Abrahams, BoostPro Computing"

  1. 1. C++ 2011 Highlights from the New International Standard 1Monday, September 19, 2011
  2. 2. Intro • Who am I • Who are you? • Why We’re Here 2Monday, September 19, 2011
  3. 3. C++11 Goals • Make C++ a better language for systems programming • Make C++ a better language for writing libraries • Make C++ more teachable and learnable • Maintain backward compatibility 3Monday, September 19, 2011
  4. 4. C++11 At a Glance • General Core Language Features concurrency, move semantics, auto, range-based for, lambdas… • Library Features containers, regular expressions, smart pointers, new algorithms… • Features for writing classes constructor delegation, override/final, =default/=delete… • Crazy Template Stuff Variadic templates, template aliases, decltype, perfect forwarding, … 4Monday, September 19, 2011
  5. 5. Ponies for Everybody! Big wins from little features 5Monday, September 19, 2011
  6. 6. Type Deduction void print_3x(vector<float> const& v) { for ( vector<float>::const_iterator p = v.begin(); p != v.end(); p++ ) { std::cout << *p * 3 << " "; } } 6Monday, September 19, 2011
  7. 7. Type Deduction void print_3x(vector<float> const& v) { for ( vector<float>::const_iterator p = v.begin(); p != v.end(); p++ ) { std::cout << *p * 3 << " "; } } 6Monday, September 19, 2011
  8. 8. Type Deduction void print_3x(vector<float> const& v) { for ( auto p = v.begin(); p != v.end(); p++ ) { std::cout << *p * 3 << " "; } } 7Monday, September 19, 2011
  9. 9. Type Deduction void print_3x(vector<float> const& v) { for ( auto p = v.begin(); p != v.end(); p++ ) { std::cout << *p * 3 << " "; } } 8Monday, September 19, 2011
  10. 10. Type Deduction auto a = 3; int const auto b = 3.14; const double auto const& c = 42; int const& auto d = "foobar"; char const* auto& e = "baz" char const(&)[4] extern std::list<Foo> l; auto p = l.begin(); std::list<Foo>::iterator auto& x = l.front(); std::list<Foo>::value_type& auto& y = any-expression-here; 9Monday, September 19, 2011
  11. 11. Type Deduction void print_3x(vector<float> const& v) { for ( auto p = v.begin(); p != v.end(); p++ ) { std::cout << *p * 3 << " "; } } 10Monday, September 19, 2011
  12. 12. Range-based for loop void print_3x(vector<float> const& v) { for ( auto p = v.begin(); p != v.end(); p++ ) { std::cout << *p * 3 << " "; } } 11Monday, September 19, 2011
  13. 13. Range-based for loop void print_3x(vector<float> const& v) { for ( float a: v ) { std::cout << a * 3 << " "; } } 12Monday, September 19, 2011
  14. 14. Range-based for loop void print_3x(vector<float> const& v) Range { for ( float a: v ) { std::cout << a * 3 << " "; } } 12Monday, September 19, 2011
  15. 15. Range-based for loop void print_3x(vector<float> const& v) Loop variable Range { for ( float a: v ) { std::cout << a * 3 << " "; } } 12Monday, September 19, 2011
  16. 16. Range-based for loop auto in C++11 void print_3x(vector<float> const& v) { for ( auto a: v ) { std::cout << a * 3 << " "; } } 13Monday, September 19, 2011
  17. 17. Range-based for loop template <class Range> void print_3x(Range const& v) { for ( typename Range::value_type a: v ) { std::cout << a * 3 << " "; } } 14Monday, September 19, 2011
  18. 18. Range-based for loop auto in C++11 template <class Range> void print_3x(Range const& v) { for ( auto a: v ) { std::cout << a * 3 << " "; } } 15Monday, September 19, 2011
  19. 19. Range-based for loop auto in C++11 void inplace_3x(vector<float>& v) { for ( float& a: v ) { a *= 3; } } 16Monday, September 19, 2011
  20. 20. Ranges Everywhere int rng[3] = { 42, 314, 77 }; for( int var: rng ) { ... } 17Monday, September 19, 2011
  21. 21. Ranges Everywhere std::pair<Iter,Iter> rng = … ; for( int var: rng ) { ... } 18Monday, September 19, 2011
  22. 22. Ranges Everywhere YourType rng; for( int var: rng ) { ... } 19Monday, September 19, 2011
  23. 23. 98 ++Range-based for loop C void print_3x(vector<float> const& v) { BOOST_FOREACH( float a, v ) { std::cout << a * 3 << " "; } } 20Monday, September 19, 2011
  24. 24. 98 ++Range-based“for”loop C void print_3x(vector<float> const& v) { BOOST_FOREACH( float a, v ) { std::cout << a * 3 << " "; } } 20Monday, September 19, 2011
  25. 25. 98 ++Range-based“for”loop C void print_3x(vector<float> const& v) { BOOST_FOREACH( float a, v ) { std::cout << a * 3 << " "; } } See your friendly neighborhood Boost C++ Libraries for details 20Monday, September 19, 2011
  26. 26. Using an Algorithm void print_3x(vector<float> const& v) { std::for_each( v.begin(), v.end(), ); } 21Monday, September 19, 2011
  27. 27. Using an Algorithm void print_3x(vector<float> const& v) { std::for_each( v.begin(), v.end(), ); } 21Monday, September 19, 2011
  28. 28. Using an Algorithm void print_3x(vector<float> const& v) { std::for_each( v.begin(), v.end(), ); } 21Monday, September 19, 2011
  29. 29. Using an Algorithm static void print_3x_element(float a) { std::cout << a * 3 << " "; } void print_3x(vector<float> const& v) { std::for_each( v.begin(), v.end(), ); } 21Monday, September 19, 2011
  30. 30. Using an Algorithm static void print_3x_element(float a) { std::cout << a * 3 << " "; } void print_3x(vector<float> const& v) { std::for_each( v.begin(), v.end(), print_3x_element); } 21Monday, September 19, 2011
  31. 31. Using an Algorithm static void print_3x_element(float a) { std::cout << a * 3 << " "; } void print_3x(vector<float> const& v) { std::for_each( v.begin(), v.end(), print_3x_element); } 21Monday, September 19, 2011
  32. 32. Lambda Expressions void print_3x(vector<float> const& v) { std::for_each( v.begin(), v.end(), ); } 22Monday, September 19, 2011
  33. 33. Lambda Expressions void print_3x(vector<float> const& v) { std::for_each( v.begin(), v.end(), [](float a) { std::cout << a * 3 << " "; } ); } “Lambda expression” 22Monday, September 19, 2011
  34. 34. Lambda Expressions void print_3x(vector<float> const& v) { std::for_each( v.begin(), v.end(), [](float a) { std::cout << a * 3 << " "; } ); } 23Monday, September 19, 2011
  35. 35. Lambda Expressions void print_3x(vector<float> const& v) { std::for_each( v.begin(), v.end(), [](float a) { std::cout << a * 3 << " "; } ); } introducer 23Monday, September 19, 2011
  36. 36. What It Is void print_3x(vector<float> const& v) { std::for_each( v.begin(), v.end(), [](float a) { std::cout << a * 3 << " "; } ); } 24Monday, September 19, 2011
  37. 37. anonymous class What It Is struct __lambda143 { inline void operator()(float a) const { std::cout << a * 3 << " "; } }; void print_3x(vector<float> const& v) { std::for_each( v.begin(), v.end(), [](float a) { std::cout << a * 3 << " "; } ); } 24Monday, September 19, 2011
  38. 38. anonymous class What It Is struct __lambda143 { inline void operator()(float a) const { std::cout << a * 3 << " "; } }; void print_3x(vector<float> const& v) { std::for_each( v.begin(), v.end(), [](float a) { std::cout << a * 3 << " "; } ); } 24Monday, September 19, 2011
  39. 39. anonymous class What It Is struct __lambda143 { inline void operator()(float a) const { std::cout << a * 3 << " "; } }; void print_3x(vector<float> const& v) { std::for_each( v.begin(), v.end(), __lambda143() [](float a) { std::cout << a * 3 << " "; } ); } “closure” instance 24Monday, September 19, 2011
  40. 40. Closing Angle Brackets std::vector<std::pair<int, std::string>> x; 25Monday, September 19, 2011
  41. 41. Closing Angle Brackets Error in C++98: >> tokenized as right-shift std::vector<std::pair<int, std::string>> x; 25Monday, September 19, 2011
  42. 42. Closing Angle Brackets std::vector<std::pair<int, std::string>> x; OK in C++11 25Monday, September 19, 2011
  43. 43. Move Semantics A Really Cheap Lunch 26Monday, September 19, 2011
  44. 44. Copy Fatigue? typedef set<string> record; typedef vector<record> database; database load_db(string filename); database db = load_db("huge.db"); db.push_back( create_record() ); 27Monday, September 19, 2011
  45. 45. Copy Fatigue? typedef set<string> record; typedef vector<record> database; database load_db(string filename); database db = load_db("huge.db"); db.push_back( create_record() ); 27Monday, September 19, 2011
  46. 46. Copy Fatigue? typedef set<string> record; typedef vector<record> database; database load_db(string filename); database db = load_db("huge.db"); db.push_back( create_record() ); 27Monday, September 19, 2011
  47. 47. Copy Fatigue? typedef set<string> record; typedef vector<record> database; database load_db(string filename); database db = load_db("huge.db"); db.push_back( create_record() ); • A 27Monday, September 19, 2011
  48. 48. Copy Fatigue? typedef set<string> record; typedef vector<record> database; database load_db(string filename); database db = load_db("huge.db"); db.push_back( create_record() ); • A B 27Monday, September 19, 2011
  49. 49. Copy Fatigue? typedef set<string> record; typedef vector<record> database; database load_db(string filename); database db = load_db("huge.db"); db.push_back( create_record() ); • A B C 27Monday, September 19, 2011
  50. 50. Copy Fatigue? typedef set<string> record; typedef vector<record> database; database load_db(string filename); database db = load_db("huge.db"); db.push_back( create_record() ); • A B C D 27Monday, September 19, 2011
  51. 51. Copy Fatigue? typedef set<string> record; typedef vector<record> database; database load_db(string filename); database db = load_db("huge.db"); db.push_back( create_record() ); • A B C D 27Monday, September 19, 2011
  52. 52. Copy Fatigue? typedef set<string> record; typedef vector<record> database; database load_db(string filename); database db = load_db("huge.db"); db.push_back( create_record() ); • A B C D A 27Monday, September 19, 2011
  53. 53. Copy Fatigue? typedef set<string> record; typedef vector<record> database; database load_db(string filename); database db = load_db("huge.db"); db.push_back( create_record() ); • A B C D A B 27Monday, September 19, 2011
  54. 54. Copy Fatigue? typedef set<string> record; typedef vector<record> database; database load_db(string filename); database db = load_db("huge.db"); db.push_back( create_record() ); • A B C D A B C 27Monday, September 19, 2011
  55. 55. Copy Fatigue? typedef set<string> record; typedef vector<record> database; database load_db(string filename); database db = load_db("huge.db"); db.push_back( create_record() ); • A B C D A B C D 27Monday, September 19, 2011
  56. 56. Copy Fatigue? typedef set<string> record; typedef vector<record> database; database load_db(string filename); database db = load_db("huge.db"); db.push_back( create_record() ); • A B C D A B C D 28Monday, September 19, 2011
  57. 57. Copy Fatigue? typedef set<string> record; typedef vector<record> database; database load_db(string filename); database db = load_db("huge.db"); db.push_back( create_record() ); • A B C D 28Monday, September 19, 2011
  58. 58. Copy Fatigue? typedef set<string> record; typedef vector<record> database; database load_db(string filename); database db = load_db("huge.db"); db.push_back( create_record() ); • A B C D 28Monday, September 19, 2011
  59. 59. Copy Fatigue? typedef set<string> record; typedef vector<record> database; database load_db(string filename); database db = load_db("huge.db"); db.push_back( create_record() ); • A B C D E 28Monday, September 19, 2011
  60. 60. Copy Fatigue? typedef set<string> record; typedef vector<record> database; database load_db(string filename); database db = load_db("huge.db"); db.push_back( create_record() ); • A B C D E 28Monday, September 19, 2011
  61. 61. Copy Fatigue? typedef set<string> record; typedef vector<record> database; database load_db(string filename); Expensive database db = load_db("huge.db"); copies are made just before db.push_back( create_record() ); destroying the source object • A B C D E 28Monday, September 19, 2011
  62. 62. Try Moving Instead! typedef set<string> record; typedef vector<record> database; database load_db(string filename); database db = load_db("huge.db"); db.push_back( create_record() ); • A B C D 29Monday, September 19, 2011
  63. 63. Try Moving Instead! typedef set<string> record; typedef vector<record> database; database load_db(string filename); database db = load_db("huge.db"); db.push_back( create_record() ); • B C D A 29Monday, September 19, 2011
  64. 64. Try Moving Instead! typedef set<string> record; typedef vector<record> database; database load_db(string filename); database db = load_db("huge.db"); db.push_back( create_record() ); • C D A B 29Monday, September 19, 2011
  65. 65. Try Moving Instead! typedef set<string> record; typedef vector<record> database; database load_db(string filename); database db = load_db("huge.db"); db.push_back( create_record() ); • D A B C 29Monday, September 19, 2011
  66. 66. Try Moving Instead! typedef set<string> record; typedef vector<record> database; database load_db(string filename); database db = load_db("huge.db"); db.push_back( create_record() ); • A B C D 29Monday, September 19, 2011
  67. 67. Try Moving Instead! typedef set<string> record; typedef vector<record> database; database load_db(string filename); database db = load_db("huge.db"); db.push_back( create_record() ); • A B C D 29Monday, September 19, 2011
  68. 68. Try Moving Instead! typedef set<string> record; typedef vector<record> database; database load_db(string filename); database db = load_db("huge.db"); db.push_back( create_record() ); • A B C D 30Monday, September 19, 2011
  69. 69. Try Moving Instead! typedef set<string> record; typedef vector<record> database; database load_db(string filename); database db = load_db("huge.db"); db.push_back( create_record() ); • A B C D 30Monday, September 19, 2011
  70. 70. Try Moving Instead! typedef set<string> record; typedef vector<record> database; database load_db(string filename); database db = load_db("huge.db"); db.push_back( create_record() ); • A B C D E 30Monday, September 19, 2011
  71. 71. Try Moving Instead! typedef set<string> record; typedef vector<record> database; database load_db(string filename); database db = load_db("huge.db"); db.push_back( create_record() ); • A B C D E 30Monday, September 19, 2011
  72. 72. Move Mini-HOWTO Move-enabling an existing class 31Monday, September 19, 2011
  73. 73. Move-Enabling struct Owner { • X Owner(const Owner& src) : p(src.p ? new Resource(*src.p) : 0) {} Owner& operator=(const Owner& src) { if (this == &src) return *this; delete p; p = 0; if (src.p) p = new Resource(*src.p); return *this; } ~Owner() { delete p; } private: Resource* p; }; 32Monday, September 19, 2011
  74. 74. Move-Enabling struct Owner { • X Owner(const Owner& src) : p(src.p ? new Resource(*src.p) : 0) {} Owner& operator=(const Owner& src) { if (this == &src) return *this; delete p; p = 0; if (src.p) p = new Resource(*src.p); return *this; } ~Owner() { delete p; } private: Resource* p; }; 32Monday, September 19, 2011
  75. 75. Move-Enabling struct Owner { • X Owner(const Owner& src) : p(src.p ? new Resource(*src.p) : 0) {} Owner& operator=(const Owner& src) { if (this == &src) return *this; delete p; p = 0; if (src.p) p = new Resource(*src.p); return *this; } ~Owner() { delete p; } private: Resource* p; }; 32Monday, September 19, 2011
  76. 76. Move-Enabling struct Owner { • X Owner(const Owner& src) : p(src.p ? new Resource(*src.p) : 0) {} X Owner& operator=(const Owner& src) { if (this == &src) return *this; delete p; p = 0; if (src.p) p = new Resource(*src.p); return *this; } ~Owner() { delete p; } private: Resource* p; }; 32Monday, September 19, 2011
  77. 77. Move-Enabling struct Owner { • X Owner(const Owner& src) : p(src.p ? new Resource(*src.p) : 0) {} X Owner& operator=(const Owner& src) { if (this == &src) return *this; delete p; p = 0; if (src.p) p = new Resource(*src.p); return *this; } ~Owner() { delete p; } private: Resource* p; }; 32Monday, September 19, 2011
  78. 78. Move-Enabling struct Owner { • X Owner(const Owner& src) : p(src.p ? new Resource(*src.p) : 0) {} • X Owner& operator=(const Owner& src) { if (this == &src) return *this; delete p; p = 0; if (src.p) p = new Resource(*src.p); return *this; } ~Owner() { delete p; } private: Resource* p; }; 32Monday, September 19, 2011
  79. 79. Move-Enabling struct Owner { Owner(const Owner& src) : p(src.p ? new Resource(*src.p) : 0) {} Owner& operator=(const Owner& src) { if (this == &src) return *this; • Y delete p; p = 0; if (src.p) p = new Resource(*src.p); • Z return *this; } ~Owner() { delete p; } private: Resource* p; }; 33Monday, September 19, 2011
  80. 80. Move-Enabling struct Owner { Owner(const Owner& src) : p(src.p ? new Resource(*src.p) : 0) {} Owner& operator=(const Owner& src) { if (this == &src) return *this; • Y delete p; p = 0; if (src.p) p = new Resource(*src.p); • Z return *this; } ~Owner() { delete p; } private: Resource* p; }; 33Monday, September 19, 2011
  81. 81. Move-Enabling struct Owner { Owner(const Owner& src) : p(src.p ? new Resource(*src.p) : 0) {} Owner& operator=(const Owner& src) { if (this == &src) return *this; • Y delete p; p = 0; if (src.p) p = new Resource(*src.p); • Z return *this; } ~Owner() { delete p; } private: Resource* p; }; 33Monday, September 19, 2011
  82. 82. Move-Enabling struct Owner { Owner(const Owner& src) : p(src.p ? new Resource(*src.p) : 0) {} Owner& operator=(const Owner& src) { if (this == &src) return *this; • Y delete p; p = 0; if (src.p) p = new Resource(*src.p); • return *this; } ~Owner() { delete p; } private: Resource* p; }; 33Monday, September 19, 2011
  83. 83. Move-Enabling struct Owner { Owner(const Owner& src) : p(src.p ? new Resource(*src.p) : 0) {} Owner& operator=(const Owner& src) { if (this == &src) return *this; • Y delete p; p = 0; if (src.p) p = new Resource(*src.p); • return *this; } ~Owner() { delete p; } private: Resource* p; }; 33Monday, September 19, 2011
  84. 84. Move-Enabling struct Owner { Owner(const Owner& src) : p(src.p ? new Resource(*src.p) : 0) {} Owner& operator=(const Owner& src) { if (this == &src) return *this; • Y delete p; p = 0; if (src.p) p = new Resource(*src.p); • return *this; } ~Owner() { delete p; } private: Resource* p; }; 33Monday, September 19, 2011
  85. 85. Move-Enabling struct Owner { Owner(const Owner& src) : p(src.p ? new Resource(*src.p) : 0) {} Owner& operator=(const Owner& src) { if (this == &src) return *this; • Y delete p; p = 0; if (src.p) p = new Resource(*src.p); • Y return *this; } ~Owner() { delete p; } private: Resource* p; }; 33Monday, September 19, 2011
  86. 86. Move-Enabling struct Owner { Owner(const Owner& src) : p(src.p ? new Resource(*src.p) : 0) {} Owner& operator=(const Owner& src) { if (this == &src) return *this; • Y delete p; p = 0; if (src.p) p = new Resource(*src.p); • Y return *this; } ~Owner() { delete p; } private: Resource* p; }; 33Monday, September 19, 2011
  87. 87. New Move Operations struct Owner { Owner(const Owner& src) : p(src.p ? new Resource(*src.p) : 0) {} Owner& operator=(const Owner& src) { if (this == &src) return *this; delete p; p = 0; if (src.p) p = new Resource(*src.p); return *this; } ~Owner() { delete p; } private: Resource* p; }; 34Monday, September 19, 2011
  88. 88. New Move Operations struct Owner { Owner(const Owner& src) Owner& operator=(const Owner& src) ~Owner() { delete p; } private: Resource* p; }; 34Monday, September 19, 2011
  89. 89. New Move Operations struct Owner { Owner(const Owner& src) Owner& operator=(const Owner& src) ~Owner() { delete p; } private: Resource* p; }; 34Monday, September 19, 2011
  90. 90. Move Constructor struct Owner { Owner(const Owner& src); Owner& operator=(const Owner& src); • X ~Owner() { delete p; } private: Resource* p; }; 35Monday, September 19, 2011
  91. 91. Move Constructor struct Owner { Owner(const Owner& src); Owner& operator=(const Owner& src); • X ~Owner() { delete p; } private: Resource* p; }; 35Monday, September 19, 2011
  92. 92. Move Constructor struct Owner { Owner(const Owner& src); Owner& operator=(const Owner& src); Owner(Owner&& src) : p(src.p) { src.p = 0; } • X ~Owner() { delete p; } private: Resource* p; }; 35Monday, September 19, 2011
  93. 93. Move Constructor struct Owner { Owner(const Owner& src); Owner& operator=(const Owner& src); Owner(Owner&& src) : p(src.p) { src.p = 0; } • X rvalue reference ~Owner() { delete p; } private: Resource* p; }; 35Monday, September 19, 2011
  94. 94. Move Constructor struct Owner { Owner(const Owner& src); Owner& operator=(const Owner& src); Owner(Owner&& src) : p(src.p) { src.p = 0; } • X rvalue reference ~Owner() { delete p; } private: Resource* p; }; 35Monday, September 19, 2011
  95. 95. Move Constructor struct Owner { Owner(const Owner& src); Owner& operator=(const Owner& src); Owner(Owner&& src) : p(src.p) { src.p = 0; } • X ~Owner() { delete p; } private: Resource* p; }; 35Monday, September 19, 2011
  96. 96. Move Constructor struct Owner { Owner(const Owner& src); Owner& operator=(const Owner& src); Owner(Owner&& src) : p(src.p) { src.p = 0; } • X • ~Owner() { delete p; } private: Resource* p; }; 35Monday, September 19, 2011
  97. 97. Move Constructor struct Owner { Owner(const Owner& src); Owner& operator=(const Owner& src); Owner(Owner&& src) : p(src.p) { src.p = 0; } • X • ~Owner() { delete p; } private: Resource* p; }; 35Monday, September 19, 2011
  98. 98. Move Constructor struct Owner { Owner(const Owner& src); Owner& operator=(const Owner& src); Owner(Owner&& src) : p(src.p) { src.p = 0; } • X • ~Owner() { delete p; } private: Resource* p; }; 35Monday, September 19, 2011
  99. 99. Move Assignment struct Owner { Owner(const Owner& src); Owner& operator=(const Owner& src); Owner(Owner&& src) : p(src.p) { src.p = 0; } • Y • Z ~Owner() { delete p; } private: Resource* p; }; 36Monday, September 19, 2011
  100. 100. Move Assignment struct Owner { Owner(const Owner& src); Owner& operator=(const Owner& src); Owner(Owner&& src) : p(src.p) { src.p = 0; } Owner& operator=(Owner&& src) { if (this == &src) return *this; • Y delete p; p = src.p; src.p = 0; • Z return *this; } ~Owner() { delete p; } private: Resource* p; }; 36Monday, September 19, 2011
  101. 101. Move Assignment struct Owner { Owner(const Owner& src); Owner& operator=(const Owner& src); Owner(Owner&& src) : p(src.p) { src.p = 0; } Owner& operator=(Owner&& src) { if (this == &src) return *this; • Y delete p; p = src.p; src.p = 0; • Z return *this; } ~Owner() { delete p; } private: Resource* p; }; 36Monday, September 19, 2011
  102. 102. Move Assignment struct Owner { Owner(const Owner& src); Owner& operator=(const Owner& src); Owner(Owner&& src) : p(src.p) { src.p = 0; } Owner& operator=(Owner&& src) { if (this == &src) return *this; • Y delete p; p = src.p; src.p = 0; • Z return *this; } ~Owner() { delete p; } private: Resource* p; }; 36Monday, September 19, 2011
  103. 103. Move Assignment struct Owner { Owner(const Owner& src); Owner& operator=(const Owner& src); Owner(Owner&& src) : p(src.p) { src.p = 0; } Owner& operator=(Owner&& src) { if (this == &src) return *this; • Y delete p; p = src.p; src.p = 0; • return *this; } ~Owner() { delete p; } private: Resource* p; }; 36Monday, September 19, 2011
  104. 104. Move Assignment struct Owner { Owner(const Owner& src); Owner& operator=(const Owner& src); Owner(Owner&& src) : p(src.p) { src.p = 0; } Owner& operator=(Owner&& src) { if (this == &src) return *this; • Y delete p; p = src.p; src.p = 0; • return *this; } ~Owner() { delete p; } private: Resource* p; }; 36Monday, September 19, 2011
  105. 105. Move Assignment struct Owner { Owner(const Owner& src); Owner& operator=(const Owner& src); Owner(Owner&& src) : p(src.p) { src.p = 0; } Owner& operator=(Owner&& src) { if (this == &src) return *this; • delete p; p = src.p; src.p = 0; • Y return *this; } ~Owner() { delete p; } private: Resource* p; }; 36Monday, September 19, 2011
  106. 106. Move Semantics • Automatically accelerates existing code! • Applied throughout standard library • Many move-only types in library • Launches a new era of value semantics 37Monday, September 19, 2011
  107. 107. Containers • Move semantics • Hash containers • forward_list • array • tuple • Emplace • Allocators 38Monday, September 19, 2011
  108. 108. Other Useful Libraries • regex: • Algorithms • function / bind 39Monday, September 19, 2011
  109. 109. Smart Pointers • unique_ptr • shared_ptr 40Monday, September 19, 2011
  110. 110. Classes and OOP • member initializers • explict conversion • override and final • delegating and inheriting constructors • defaulted and deleted functions 41Monday, September 19, 2011
  111. 111. Concurrency • async, futures, promise • locks, mutexes and condition variables • exception propagation • atomics 42Monday, September 19, 2011
  112. 112. Templates • Variadics • static_assert • local classes as template arguments • extern templates • Template aliases 43Monday, September 19, 2011
  113. 113. Metaprogramming • constexpr • Template aliases • User-defined literals w/variadic templates • Type traits and extended SFINAE 44Monday, September 19, 2011
  114. 114. What’s Next? • Modules • Concepts • Pythy Syntax: • []min(x, y) { return x < y ? x : y } 45Monday, September 19, 2011
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×