ebook img

C++ and Beyond PDF

258 Pages·26.515 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview C++ and Beyond

artima Image: NASA C++ and Beyond 2010 Artima Press is an imprint of Artima, Inc. P.O. Box 305, Walnut Creek, California 94597 Copyright © 2011 Scott Meyers, Herb Sutter, Andrei Alexandrescu. All rights reserved. Published March 17, 2011 Produced in the United States of America No part of this publication may be reproduced, modified, distributed, stored in a re- trieval system, republished, displayed, or performed, for commercial or noncommer- cial purposes or for compensation of any kind without prior written permission from Artima, Inc. This PDF eBook is prepared exclusively for its purchaser, who may use it for personal purposes only, as described by the Artima eBook license (http://www.artima.com/ ebook_license.html). In addition, the purchaser may modify this PDF eBook to high- light sections, add comments and annotations, etc., except that the “For the exclusive use of ” text that identifies the purchaser may not be modified in any way. All information and materials in this eBook are provided “as is” and without warranty of any kind. The term “Artima” and the Artima logo are trademarks or registered trademarks of Artima, Inc. All other company and/or product names may be trademarks or registered trademarks of their owners. Download from Wow! eBook <www.wowebook.com> C++ and Beyond Snoqualmie, Washington, USA October 24-27, 2010 Schedule Sunday Monday Tuesday Wednesday Group Breakfast Group Breakfast Group Breakfast 8:00 - 9:00 [Attic] [Attic] [Attic] Welcome[Andrei] Move Semantics, Rvalue A Fresh Look at Containers Elements of Design, Part 1 9:00 - 10:30 References, and Perfect and Iterators [Herb] Forwarding, Part 1 [Andrei] [Scott] Break Break Break Move Semantics, Rvalue References, and Perfect CAS-Based Concurrency Elements of Design, Part 2 10:45 - 12:00 Forwarding, Part 2 [Andrei] [Herb] [Scott] Group Lunch Group Lunch Group Lunch 12:00 - 2:30 and and and Mid-Day Activity Mid-Day Activity Mid-Day Activity Super Size Me: Lessons CPU Caches and Lambdas, Lambdas, Learned Working at a Web 2:30 - 3:45 Why You Care Everywhere Company [Scott] [Herb] [Andrei] Break Break Break Points and Counterpoints Ask us anything...live! 4:00 - 5:00 Ask us anything...in advance [Panel] [Panel] [Panel] Free Time 5:00 - 7:30 (No Official C&B-Related Activities) Reception Informal Discussions Informal Discussions 7:30 - 9:30 [Ballroom] [Falls Terrace & Ballroom] [Falls Terrace & Ballroom] Download from Wow! eBook <www.wowebook.com> C++ and Beyond Snoqualmie, Washington, USA December 13-16, 2010 Schedule Monday Tuesday Wednesday Thursday Group Breakfast Group Breakfast Group Breakfast 8:00 - 8:45 [Attic] [Attic] [Attic] 8:45 - 9:00 Welcome Announcements Announcements Move Semantics, Rvalue References, and Perfect Elements of Design, Part 1 Scalable Use of the STL 9:00 - 10:30 Forwarding, Part 1 [Herb] [Andrei] [Scott] Break Break Break Move Semantics, Rvalue References, and Perfect CAS-Based Concurrency Elements of Design, Part 2 10:45 - 12:00 Forwarding, Part 2 [Andrei] [Herb] [Scott] Group Lunch Group Lunch Group Lunch 12:00 - 2:30 and and and Mid-Day Activity Mid-Day Activity Mid-Day Activity Super Size Me: Lessons Lambdas, Lambdas, CPU Caches and Learned Working at a Web 2:30 - 4:00 Everywhere Why You Care Company [Herb] [Scott] [Andrei] Break Break Break Informal C++0x Feature Q&A Q&A 4:15 - 5:00 Overview [Andrei, Scott, Herb] [Herb, Andrei, Scott] [Scott, Herb, Andrei] Free Time 5:00 - 7:30 (No Official C&B-Related Activities) Reception Informal Discussions Informal Discussions 7:30 - 9:30 [Falls Terrace] [Falls Terrace] [Falls Terrace] Download from Wow! eBook <www.wowebook.com> Move Semantics, Rvalue References, Perfect Forwarding Move(cid:2)Semantics,(cid:2)Rvalue(cid:2)References,(cid:2) and(cid:2)Perfect(cid:2)Forwarding Scott(cid:2)Meyers,(cid:2)Ph.D. Software(cid:2)Development(cid:2)Consultant [email protected] Voice:(cid:2)503/638(cid:3)6028 http://www.aristeia.com/ Fax:(cid:2)503/974(cid:3)1887 Scott(cid:2)Meyers,(cid:2)Software(cid:2)Development(cid:2)Consultant ©(cid:2)2010(cid:2)Scott(cid:2)Meyers,(cid:2)all(cid:2)rights(cid:2)reserved. http://www.aristeia.com/ Last(cid:2)Revised:(cid:2)12/21/10 C++0x(cid:2)Warning Some(cid:2)examples(cid:2)show(cid:2)C++0x(cid:2)features(cid:2)unrelated(cid:2)to(cid:2)move(cid:2)semantics. I’m(cid:2)sorry(cid:2)about(cid:2)that. But(cid:2)not(cid:2)that(cid:2)sorry(cid:2):(cid:3)) Scott(cid:2)Meyers,(cid:2)Software(cid:2)Development(cid:2)Consultant ©(cid:2)2010(cid:2)Scott(cid:2)Meyers,(cid:2)all(cid:2)rights(cid:2)reserved. http://www.aristeia.com/ Slide(cid:2)2 Scott Meyers, Software Development Consultant © 2010 Scott Meyers, all rights reserved. http://www.aristeia.com/ Download from Wow! eBook <www.wowebook.com> Move Semantics, Rvalue References, Perfect Forwarding Move(cid:2)Support C++(cid:2)sometimes(cid:2)performs(cid:2)unnecessary(cid:2)copying: typedef std::vector<T> TVec; TVec createTVec(); // factory function TVec vt; … vt = createTVec(); // copy return value object to vt, // then destroy return value object createTVec vt TVec T T T … T T T T T T … T T T Scott(cid:2)Meyers,(cid:2)Software(cid:2)Development(cid:2)Consultant ©(cid:2)2010(cid:2)Scott(cid:2)Meyers,(cid:2)all(cid:2)rights(cid:2)reserved. http://www.aristeia.com/ Slide(cid:2)3 Move(cid:2)Support Movingvalues(cid:2)would(cid:2)be(cid:2)cheaper: TVec vt; … vt = createTVec(); // move data in return value object // to vt, then destroy return value // object createTVec vt TVec T T T … T T T Scott(cid:2)Meyers,(cid:2)Software(cid:2)Development(cid:2)Consultant ©(cid:2)2010(cid:2)Scott(cid:2)Meyers,(cid:2)all(cid:2)rights(cid:2)reserved. http://www.aristeia.com/ Slide(cid:2)4 Scott Meyers, Software Development Consultant © 2010 Scott Meyers, all rights reserved. http://www.aristeia.com/ Download from Wow! eBook <www.wowebook.com> Move Semantics, Rvalue References, Perfect Forwarding Move(cid:2)Support Appending(cid:2)to(cid:2)a(cid:2)full(cid:2)vector causes(cid:2)much(cid:2)copying(cid:2)before(cid:2)the(cid:2)append: std::vector<T> vt; ... vt.push_back(T object); // assume vt lacks // unused capacity vt T T T … T T T e e e e e e at at at at at at St St St St St St T T T T T T T T T … T T T T e e e e e e e at at at at at at at St St St St St St St T T T T T T T Scott(cid:2)Meyers,(cid:2)Software(cid:2)Development(cid:2)Consultant ©(cid:2)2010(cid:2)Scott(cid:2)Meyers,(cid:2)all(cid:2)rights(cid:2)reserved. http://www.aristeia.com/ Slide(cid:2)5 Move(cid:2)Support Again,(cid:2)moving(cid:2)would(cid:2)be(cid:2)more(cid:2)efficient: std::vector<T> vt; ... vt.push_back(T object); // assume vt lacks // unused capacity vt T T T … T T T e e e e e e e at at at at at at at St St St St St St St T T T T T T T T T T … T T T T Other(cid:2)vector and(cid:2)deque operations(cid:2)could(cid:2)similarly(cid:2)benefit. (cid:2) insert,(cid:2)emplace,(cid:2)resize,(cid:2)erase,(cid:2)etc. Scott(cid:2)Meyers,(cid:2)Software(cid:2)Development(cid:2)Consultant ©(cid:2)2010(cid:2)Scott(cid:2)Meyers,(cid:2)all(cid:2)rights(cid:2)reserved. http://www.aristeia.com/ Slide(cid:2)6 Scott Meyers, Software Development Consultant © 2010 Scott Meyers, all rights reserved. http://www.aristeia.com/ Download from Wow! eBook <www.wowebook.com> Move Semantics, Rvalue References, Perfect Forwarding Move(cid:2)Support Still(cid:2)another(cid:2)example: template<typename T> // straightforward std::swap impl. void swap(T& a, T& b) { T tmp(a); // copy a to tmp ((cid:3514) 2 copies of a) a = b; // copy b to a ((cid:3514) 2 copies of b) b = tmp; // copy tmp to b ((cid:3514) 2 copies of tmp) } // destroy tmp a copya ’osf sbt’ast estate b copyb ’osf sat’ast estate tmp copy of a’s state Scott(cid:2)Meyers,(cid:2)Software(cid:2)Development(cid:2)Consultant ©(cid:2)2010(cid:2)Scott(cid:2)Meyers,(cid:2)all(cid:2)rights(cid:2)reserved. http://www.aristeia.com/ Slide(cid:2)7 Move(cid:2)Support template<typename T> // straightforward std::swap impl. void swap(T& a, T& b) { T tmp(std::move(a)); // move a’s data to tmp a = std::move(b); // move b’s data to a b = std::move(tmp); // move tmp’s data to b } // destroy (eviscerated) tmp a a’s state tmp b b’s state Scott(cid:2)Meyers,(cid:2)Software(cid:2)Development(cid:2)Consultant ©(cid:2)2010(cid:2)Scott(cid:2)Meyers,(cid:2)all(cid:2)rights(cid:2)reserved. http://www.aristeia.com/ Slide(cid:2)8 Scott Meyers, Software Development Consultant © 2010 Scott Meyers, all rights reserved. http://www.aristeia.com/ Download from Wow! eBook <www.wowebook.com> Move Semantics, Rvalue References, Perfect Forwarding Move(cid:2)Support Moving(cid:2)most(cid:2)important(cid:2)when: (cid:2) Object(cid:2)has(cid:2)data(cid:2)in(cid:2)separate(cid:2)memory(cid:2)(e.g.,(cid:2)on(cid:2)heap). (cid:2) Copying(cid:2)is(cid:2)deep. Moving(cid:2)copies(cid:2)only(cid:2)object(cid:2)memory. (cid:2) Copying(cid:2)copies(cid:2)object(cid:2)memory(cid:2)+(cid:2)separate(cid:2)memory. Consider(cid:2)copying/moving(cid:2)A(cid:2)to(cid:2)B: A copBi ed A copBied A copBied c c data Ad daatata A B A moBved A moBved m m data data Moving(cid:2)never(cid:2)slower(cid:2)than(cid:2)copying,(cid:2)and(cid:2)often(cid:2)faster. Scott(cid:2)Meyers,(cid:2)Software(cid:2)Development(cid:2)Consultant ©(cid:2)2010(cid:2)Scott(cid:2)Meyers,(cid:2)all(cid:2)rights(cid:2)reserved. http://www.aristeia.com/ Slide(cid:2)9 Performance(cid:2)Data Consider(cid:2)these(cid:2)use(cid:2)cases(cid:2)again: vt = createTVec(); // return/assignment vt.push_back(T object); // push_back Copy(cid:3)vs(cid:3)move(cid:2)performance(cid:2)differences(cid:2)notable: Scott(cid:2)Meyers,(cid:2)Software(cid:2)Development(cid:2)Consultant ©(cid:2)2010(cid:2)Scott(cid:2)Meyers,(cid:2)all(cid:2)rights(cid:2)reserved. http://www.aristeia.com/ Slide(cid:2)10 Scott Meyers, Software Development Consultant © 2010 Scott Meyers, all rights reserved. http://www.aristeia.com/ Download from Wow! eBook <www.wowebook.com> Move Semantics, Rvalue References, Perfect Forwarding Move(cid:2)Support Lets(cid:2)C++(cid:2)recognize(cid:2)move(cid:2)opportunities(cid:2)and(cid:2)take(cid:2)advantage(cid:2)of(cid:2)them. (cid:2) How(cid:2)recognize(cid:2)them? (cid:2) How(cid:2)take(cid:2)advantage(cid:2)of(cid:2)them? Scott(cid:2)Meyers,(cid:2)Software(cid:2)Development(cid:2)Consultant ©(cid:2)2010(cid:2)Scott(cid:2)Meyers,(cid:2)all(cid:2)rights(cid:2)reserved. http://www.aristeia.com/ Slide(cid:2)11 Lvalues(cid:2)and(cid:2)Rvalues Lvaluesare(cid:2)generally(cid:2)things(cid:2)you(cid:2)can(cid:2)take(cid:2)the(cid:2)address(cid:2)of: (cid:2) Named(cid:2)objects. (cid:2) Lvalue(cid:2)references. (cid:2)More(cid:2)on(cid:2)this(cid:2)term(cid:2)in(cid:2)a(cid:2)moment. Rvaluesare(cid:2)generally(cid:2)things(cid:2)you(cid:2)can’t(cid:2)take(cid:2)the(cid:2)address(cid:2)of. (cid:2) Typically(cid:2)unnamed(cid:2)temporary(cid:2)objects. Examples: int x, *pInt; // x, pInt, *pInt are lvalues std::size_t f(std::string str); // str is lvalue, f's return is rvalue f("Hello"); // temp string created for call // is rvalue std::vector<int> vi; // vi is lvalue … vi[5] = 0; // vi[5] is lvalue (cid:2)Recall(cid:2)that(cid:2)vector<T>::operator[] returns(cid:2)T&. Scott(cid:2)Meyers,(cid:2)Software(cid:2)Development(cid:2)Consultant ©(cid:2)2010(cid:2)Scott(cid:2)Meyers,(cid:2)all(cid:2)rights(cid:2)reserved. http://www.aristeia.com/ Slide(cid:2)12 Scott Meyers, Software Development Consultant © 2010 Scott Meyers, all rights reserved. http://www.aristeia.com/ Download from Wow! eBook <www.wowebook.com>

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.