Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Everything You Ever Wanted to Know About Move Semantics, Howard Hinnant, Accu 2014

8,005 views

Published on

This talk was given by Howard Hinnant, Senior Software Engineer, Ripple Labs in April 2014. It covers the genesis of move semantics, special member functions, and an introduction to the special move members.

Published in: Engineering
  • DOWNLOAD THE BOOK INTO AVAILABLE FORMAT (New Update) ......................................................................................................................... ......................................................................................................................... Download Full PDF EBOOK here { https://urlzs.com/UABbn } ......................................................................................................................... Download Full EPUB Ebook here { https://urlzs.com/UABbn } ......................................................................................................................... Download Full doc Ebook here { https://urlzs.com/UABbn } ......................................................................................................................... Download PDF EBOOK here { https://urlzs.com/UABbn } ......................................................................................................................... Download EPUB Ebook here { https://urlzs.com/UABbn } ......................................................................................................................... Download doc Ebook here { https://urlzs.com/UABbn } ......................................................................................................................... ......................................................................................................................... ................................................................................................................................... eBook is an electronic version of a traditional print book THE can be read by using a personal computer or by using an eBook reader. (An eBook reader can be a software application for use on a computer such as Microsoft's free Reader application, or a book-sized computer THE is used solely as a reading device such as Nuvomedia's Rocket eBook.) Users can purchase an eBook on diskette or CD, but the most popular method of getting an eBook is to purchase a downloadable file of the eBook (or other reading material) from a Web site (such as Barnes and Noble) to be read from the user's computer or reading device. Generally, an eBook can be downloaded in five minutes or less ......................................................................................................................... .............. Browse by Genre Available eBOOK .............................................................................................................................. Art, Biography, Business, Chick Lit, Children's, Christian, Classics, Comics, Contemporary, CookBOOK, Manga, Memoir, Music, Mystery, Non Fiction, Paranormal, Philosophy, Poetry, Psychology, Religion, Romance, Science, Science Fiction, Self Help, Suspense, Spirituality, Sports, Thriller, Travel, Young Adult, Crime, EBOOK, Fantasy, Fiction, Graphic Novels, Historical Fiction, History, Horror, Humor And Comedy, ......................................................................................................................... ......................................................................................................................... .....BEST SELLER FOR EBOOK RECOMMEND............................................................. ......................................................................................................................... Blowout: Corrupted Democracy, Rogue State Russia, and the Richest, Most Destructive Industry on Earth,-- The Ride of a Lifetime: Lessons Learned from 15 Years as CEO of the Walt Disney Company,-- Call Sign Chaos: Learning to Lead,-- StrengthsFinder 2.0,-- Stillness Is the Key,-- She Said: Breaking the Sexual Harassment Story THE Helped Ignite a Movement,-- Atomic Habits: An Easy & Proven Way to Build Good Habits & Break Bad Ones,-- Everything Is Figureoutable,-- What It Takes: Lessons in the Pursuit of Excellence,-- Rich Dad Poor Dad: What the Rich Teach Their Kids About Money THE the Poor and Middle Class Do Not!,-- The Total Money Makeover: Classic Edition: A Proven Plan for Financial Fitness,-- Shut Up and Listen!: Hard Business Truths THE Will Help You Succeed, ......................................................................................................................... .........................................................................................................................
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Hi there! I just wanted to share a list of sites that helped me a lot during my studies: .................................................................................................................................... www.EssayWrite.best - Write an essay .................................................................................................................................... www.LitReview.xyz - Summary of books .................................................................................................................................... www.Coursework.best - Online coursework .................................................................................................................................... www.Dissertations.me - proquest dissertations .................................................................................................................................... www.ReMovie.club - Movies reviews .................................................................................................................................... www.WebSlides.vip - Best powerpoint presentations .................................................................................................................................... www.WritePaper.info - Write a research paper .................................................................................................................................... www.EddyHelp.com - Homework help online .................................................................................................................................... www.MyResumeHelp.net - Professional resume writing service .................................................................................................................................. www.HelpWriting.net - Help with writing any papers ......................................................................................................................................... Save so as not to lose
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • HelpWriting.net This service will write as best as they can. So you do not need to waste the time on rewritings.
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Unlock The Universe & Get Answers You Seek Today In Your FREE Tarot Reading. DO THIS FIRST... To get the most out of your tarot reading, I first need you to focus your intention - this concentrates the energy on the universe to answer the questions that you most desire the answers for. Take 10 seconds to think of your #1 single biggest CHALLENGE right now. (Yes, stop for 10 seconds, close your eyes, and focus your energy on ONE key problem) Ready? Okay, let's proceed. ➽➽ https://url.cn/YtemTEAx
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • DOWNLOAD THE BOOK INTO AVAILABLE FORMAT (New Update) ......................................................................................................................... ......................................................................................................................... Download Full PDF EBOOK here { https://urlzs.com/UABbn } ......................................................................................................................... Download Full EPUB Ebook here { https://urlzs.com/UABbn } ......................................................................................................................... Download Full doc Ebook here { https://urlzs.com/UABbn } ......................................................................................................................... Download PDF EBOOK here { https://urlzs.com/UABbn } ......................................................................................................................... Download EPUB Ebook here { https://urlzs.com/UABbn } ......................................................................................................................... Download doc Ebook here { https://urlzs.com/UABbn } ......................................................................................................................... ......................................................................................................................... ................................................................................................................................... eBook is an electronic version of a traditional print book THE can be read by using a personal computer or by using an eBook reader. (An eBook reader can be a software application for use on a computer such as Microsoft's free Reader application, or a book-sized computer THE is used solely as a reading device such as Nuvomedia's Rocket eBook.) Users can purchase an eBook on diskette or CD, but the most popular method of getting an eBook is to purchase a downloadable file of the eBook (or other reading material) from a Web site (such as Barnes and Noble) to be read from the user's computer or reading device. Generally, an eBook can be downloaded in five minutes or less ......................................................................................................................... .............. Browse by Genre Available eBOOK .............................................................................................................................. Art, Biography, Business, Chick Lit, Children's, Christian, Classics, Comics, Contemporary, CookBOOK, Manga, Memoir, Music, Mystery, Non Fiction, Paranormal, Philosophy, Poetry, Psychology, Religion, Romance, Science, Science Fiction, Self Help, Suspense, Spirituality, Sports, Thriller, Travel, Young Adult, Crime, EBOOK, Fantasy, Fiction, Graphic Novels, Historical Fiction, History, Horror, Humor And Comedy, ......................................................................................................................... ......................................................................................................................... .....BEST SELLER FOR EBOOK RECOMMEND............................................................. ......................................................................................................................... Blowout: Corrupted Democracy, Rogue State Russia, and the Richest, Most Destructive Industry on Earth,-- The Ride of a Lifetime: Lessons Learned from 15 Years as CEO of the Walt Disney Company,-- Call Sign Chaos: Learning to Lead,-- StrengthsFinder 2.0,-- Stillness Is the Key,-- She Said: Breaking the Sexual Harassment Story THE Helped Ignite a Movement,-- Atomic Habits: An Easy & Proven Way to Build Good Habits & Break Bad Ones,-- Everything Is Figureoutable,-- What It Takes: Lessons in the Pursuit of Excellence,-- Rich Dad Poor Dad: What the Rich Teach Their Kids About Money THE the Poor and Middle Class Do Not!,-- The Total Money Makeover: Classic Edition: A Proven Plan for Financial Fitness,-- Shut Up and Listen!: Hard Business Truths THE Will Help You Succeed, ......................................................................................................................... .........................................................................................................................
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here

Everything You Ever Wanted to Know About Move Semantics, Howard Hinnant, Accu 2014

  1. 1. Everything You Ever Wanted To Know About Move Semantics Howard Hinnant Senior Software Engineer Ripple Labs ! April 12, 2014 (and then some)
  2. 2. • The genesis of move semantics • Special member functions • Introduction to the special move members Outline
  3. 3. How Did Move Semantics Get Started? • It was all about optimizing std::vector<T>. • And everything else just rode along on its coattails.
  4. 4. vector What is std::vector? • Anatomy of a vector (simplified) vector’s data begin() end() capacity()
  5. 5. How does a vector copy? vector vector’s data vector vector’s data copy
  6. 6. How does a vector move? vector vector’s data vector nullptr nullptr nullptr copy
  7. 7. How Did Move Semantics Get Started? • Remember these fundamentals about move semantics and vector, and you will have a basic understanding of all of move semantics. • The rest is just details…
  8. 8. Outline • The genesis of move semantics • Special member functions • Introduction to the special move members
  9. 9. Special Members • What are they?
  10. 10. Special Members • Special members are those member functions that the compiler can be asked to automatically generate code for.
  11. 11. Special Members • How many special members are there? 6
  12. 12. Special Members • They are: • default constructor • destructor • copy constructor • copy assignment • move constructor • move assignment X(); ~X(); X(X const&); X& operator=(X const&); X(X&&); X& operator=(X&&);
  13. 13. Special Members • The special members can be: • not declared • implicitly declared • user declared defaulted deleted user-defined or or or
  14. 14. Special Members • What counts as user-declared? struct X { X() {} // user-declared ! ! ! }; X(); // user-declared X() = default; // user-declared X() = delete; // user-declared
  15. 15. Special Members • What is the difference between not-declared and deleted? Consider: struct X { template <class ...Args> X(Args&& ...args); ! ! }; // The default constructor // is not declared
  16. 16. Special Members • X can be default constructed by using the variadic constructor. struct X { template <class ...Args> X(Args&& ...args); ! ! }; // The default destructor // is not declared
  17. 17. Special Members • Now X() binds to the deleted default constructor instead of the variadic constructor. • X is no longer default constructible. struct X { template <class ...Args> X(Args&& ...args); ! ! }; X() = delete;
  18. 18. Special Members • Deleted members participate in overload resolution. • Members not-declared do not participate in overload resolution. struct X { template <class ...Args> X(Args&& ...args); ! ! }; X() = delete;
  19. 19. Special Members • Under what circumstances are special members implicitly provided?
  20. 20. default constructor destructor copy constructor copy assignment move constructor move assignment Nothing defaulted defaulted defaulted defaulted defaulted defaulted userdeclares compiler implicitly declares Special Members • If the user declares no special members or constructors, all 6 special members will be defaulted. • This part is no different from C++98/03
  21. 21. default constructor destructor copy constructor copy assignment move constructor move assignment Nothing defaulted defaulted defaulted defaulted defaulted defaulted userdeclares compiler implicitly declares Special Members • “defaulted” can mean “deleted” if the defaulted special member would have to do something illegal, such as call another deleted function. • Defaulted move members defined as deleted, actually behave as not declared. • No, I’m not kidding!
  22. 22. default constructor destructor copy constructor copy assignment move constructor move assignment Nothing defaulted defaulted defaulted defaulted defaulted defaulted Any constructor not declared defaulted defaulted defaulted defaulted defaulted userdeclares compiler implicitly declares Special Members • If the user declares any non-special constructor, this will inhibit the implicit declaration of the default constructor.
  23. 23. default constructor destructor copy constructor copy assignment move constructor move assignment Nothing defaulted defaulted defaulted defaulted defaulted defaulted Any constructor not declared defaulted defaulted defaulted defaulted defaulted default constructor user declared defaulted defaulted defaulted defaulted defaulted userdeclares compiler implicitly declares Special Members • A user-declared default constructor will not inhibit any other special member.
  24. 24. default constructor destructor copy constructor copy assignment move constructor move assignment Nothing defaulted defaulted defaulted defaulted defaulted defaulted Any constructor not declared defaulted defaulted defaulted defaulted defaulted default constructor user declared defaulted defaulted defaulted defaulted defaulted destructor defaulted user declared defaulted defaulted not declared not declared userdeclares compiler implicitly declares Special Members • A user-declared destructor will inhibit the implicit declaration of the move members. • The implicitly defaulted copy members are deprecated. • If you declare a destructor, declare your copy members too, even though not necessary.
  25. 25. default constructor destructor copy constructor copy assignment move constructor move assignment Nothing defaulted defaulted defaulted defaulted defaulted defaulted Any constructor not declared defaulted defaulted defaulted defaulted defaulted default constructor user declared defaulted defaulted defaulted defaulted defaulted destructor defaulted user declared defaulted defaulted not declared not declared copy constructor not declared defaulted user declared defaulted not declared not declared userdeclares compiler implicitly declares Special Members • A user-declared copy constructor will inhibit the default constructor and move members.
  26. 26. default constructor destructor copy constructor copy assignment move constructor move assignment Nothing defaulted defaulted defaulted defaulted defaulted defaulted Any constructor not declared defaulted defaulted defaulted defaulted defaulted default constructor user declared defaulted defaulted defaulted defaulted defaulted destructor defaulted user declared defaulted defaulted not declared not declared copy constructor not declared defaulted user declared defaulted not declared not declared copy assignment defaulted defaulted defaulted user declared not declared not declared userdeclares compiler implicitly declares Special Members • A user-declared copy assignment will inhibit the move members.
  27. 27. default constructor destructor copy constructor copy assignment move constructor move assignment Nothing defaulted defaulted defaulted defaulted defaulted defaulted Any constructor not declared defaulted defaulted defaulted defaulted defaulted default constructor user declared defaulted defaulted defaulted defaulted defaulted destructor defaulted user declared defaulted defaulted not declared not declared copy constructor not declared defaulted user declared defaulted not declared not declared copy assignment defaulted defaulted defaulted user declared not declared not declared move constructor not declared defaulted deleted deleted user declared not declared userdeclares compiler implicitly declares Special Members • A user-declared move member will implicitly delete the copy members.
  28. 28. default constructor destructor copy constructor copy assignment move constructor move assignment Nothing defaulted defaulted defaulted defaulted defaulted defaulted Any constructor not declared defaulted defaulted defaulted defaulted defaulted default constructor user declared defaulted defaulted defaulted defaulted defaulted destructor defaulted user declared defaulted defaulted not declared not declared copy constructor not declared defaulted user declared defaulted not declared not declared copy assignment defaulted defaulted defaulted user declared not declared not declared move constructor not declared defaulted deleted deleted user declared not declared move assignment defaulted defaulted deleted deleted not declared user declared userdeclares compiler implicitly declares Special Members
  29. 29. default constructor destructor copy constructor copy assignment Nothing defaulted defaulted defaulted defaulted Any constructor not declared defaulted defaulted defaulted default constructor user declared defaulted defaulted defaulted destructor defaulted user declared defaulted defaulted copy constructor not declared defaulted user declared defaulted copy assignment defaulted defaulted defaulted user declared userdeclares compiler implicitly declares Special Members This is C++98/03
  30. 30. default constructor destructor copy constructor copy assignment move constructor move assignment Nothing defaulted defaulted defaulted defaulted defaulted defaulted Any constructor not declared defaulted defaulted defaulted defaulted defaulted default constructor user declared defaulted defaulted defaulted defaulted defaulted destructor defaulted user declared defaulted defaulted not declared not declared copy constructor not declared defaulted user declared defaulted not declared not declared copy assignment defaulted defaulted defaulted user declared not declared not declared move constructor not declared defaulted deleted deleted user declared not declared move assignment defaulted defaulted deleted deleted not declared user declared userdeclares compiler implicitly declares Special Members
  31. 31. class X { public: ! ! ! ! ! ! }; X() = default; ~X() = default; X(X const&) = default; X& operator=(X const&) = default; X& operator=(X&&) = default; X(X&&) = default; Special Members An alternate presentation of the same information.
  32. 32. class X { public: ! ! ! ! ! ! }; X() = default; ~X() = default; X(X const&) = default; X& operator=(X const&) = default; X& operator=(X&&) = default; X(X&&) = default; Special Members An alternate presentation of the same information.
  33. 33. class X { public: ! ! ! ! ! ! }; X() = default; ~X() = default; X(X const&) = default; X& operator=(X const&) = default; Special Members An alternate presentation of the same information.
  34. 34. class X { public: ! ! ! ! ! ! }; ~X() = default; X(X const&) = default; X& operator=(X const&) = default; Special Members An alternate presentation of the same information.
  35. 35. class X { public: ! ! ! ! ! ! }; X() = default; ~X() = default; X(X const&) = default; X& operator=(X const&) = default; Special Members An alternate presentation of the same information.
  36. 36. class X { public: ! ! ! ! ! ! }; ~X() = default; X(X const&) = delete; X& operator=(X const&) = delete; X(X&&) = default; Special Members An alternate presentation of the same information.
  37. 37. class X { public: ! ! ! ! ! ! }; X() = default; ~X() = default; X(X const&) = delete; X& operator=(X const&) = delete; X& operator=(X&&) = default; Special Members An alternate presentation of the same information.
  38. 38. Outline • The genesis of move semantics • Special member functions • Introduction to the special move members
  39. 39. What does a defaulted move constructor do? class X : public Base { Member m_; ! X(X&& x) : Base(static_cast<Base&&>(x)) , m_(static_cast<Member&&>(x.m_)) {} };
  40. 40. What does a typical user- defined move constructor do? class X : public Base { Member m_; ! X(X&& x) : Base(std::move(x)) , m_(std::move(x.m_)) { x.set_to_resourceless_state(); }
  41. 41. What does a defaulted move assignment do? class X : public Base { Member m_; ! X& operator=(X&& x) { Base::operator= (static_cast<Base&&>(x)); m_ = static_cast<Member&&>(x.m_); return *this; }
  42. 42. What does a typical user- defined move assignment do? class X : public Base { Member m_; ! X& operator=(X&& x) { Base::operator=(std::move(x)); m_ = std::move(x.m_); x.set_to_resourceless_state(); return *this; }
  43. 43. Can I define one special member in terms of another? Yes.
  44. 44. Should I define one special member in terms of another? No!
  45. 45. Should I define one special member in terms of another? No! • Give each special member the tender loving care it deserves. • The entire point of move semantics is to boost performance.
  46. 46. Should I define one special member in terms of another? Case study: the copy/swap idiom class X { std::vector<int> v_; public: X& operator=(X x) { // Implements v_.swap(x.v_); // both copy and return *this; // move assignment } }; What’s not to love?
  47. 47. Should I define one special member in terms of another? Case study: the copy/swap idiom class X { std::vector<int> v_; public: X& operator=(X const& x); X& operator=(X&& x); }; I’ve written highly optimized versions of the copy and move assignment operators.
  48. 48. Should I define one special member in terms of another? Case study: the copy/swap idiom 1 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2 0% 25% 50% 75% 100% How often is lhs capacity sufficient? copy/swappenalty Best case (same speed) Average case (70% slower) Worst case (almost 8 times slower) Speed of optimized copy assignment operator vs “copy/ swap” assignment lhs always needs to reallocate vector lhs never needs to reallocate vector
  49. 49. Should I define one special member in terms of another? Case study: the copy/swap idiom How hard is it to make separate optimized copy and move assignment operators for this case?
  50. 50. Should I define one special member in terms of another? Case study: the copy/swap idiom class X { std::vector<int> v_; public: // Just keep your grubby fingers // off of the keyboard. // The defaults are optimal! ! }; What’s not to love?
  51. 51. Should I define one special member in terms of another? Case study: the copy/swap idiom But the copy/swap idiom gives me strong exception safety! Good point. Are all of your clients willing to pay a giant performance penalty for strong exception safety on assignment?
  52. 52. Should I define one special member in terms of another? Case study: the copy/swap idiom Perhaps you could interest the portion of your clients that do need strong exception safety in this generic function: template <class C> C& strong_assign(C& dest, C src) { using std::swap; swap(dest, src); return dest; }
  53. 53. Should I define one special member in terms of another? Case study: the copy/swap idiom Now clients who need speed can: x = y; And clients who need strong exception safety can: strong_assign(x, y);
  54. 54. In A Hurry? • If you don’t have time to carefully consider all 6 special members, then just delete the copy members: class X { public: X(X const&) = delete; X& operator=(X const&) = delete; };
  55. 55. Summary • Know when the compiler is defaulting or deleting special members for you, and what defaulted members will do. • Always define or delete a special member when the compiler’s implicit action is not correct. • Give tender loving care to each of the 6 special members, even if the result is to let the compiler handle it.

×