In 2010, I prepared materials for a training course on C++0x (ultimately published as Overview of the New C++ , Artima Publishing, 2010). Both those materials and my knowledge greatly benefited from the technical vetting performed by Stephan T. Lavavej, Bernhard Merkle, Stanley Friesen, Leor Zolman, Hendrik Schober, and Anthony Williams. Without their help, I would probably never have been in a position to undertake Effective Modern C++. That title, incidentally, was suggested or endorsed by several readers responding to my 18 February 2014 blog post, “ Help me name my book, ”and Andrei Alexandrescu (author of Modern C++ Design , Addison-Wesley, 2001) was kind enough to bless the title as not poaching on his terminological turf.
I'm unable to identify the origins of all the information in this book, but some sources had a relatively direct impact. Item 4's use of an undefined template to coax type information out of compilers was suggested by Stephan T. Lavavej, and Matt P. Dziubinski brought Boost.TypeIndex to my attention. In Item 5, the unsigned-std::vector::size_type
example is from Andrey Karpov's 28 February 2010 article, “ In what way can C++0x standard help you eliminate 64-bit errors.” The std::pair
/ std::pair
example in the same Item is from Stephan T. Lavavej's talk at Going Native 2012 , “STL11: Magic && Secrets.” Item 6was inspired by Herb Sutter's 12 August 2013 article, “ GotW #94 Solution: AAA Style (Almost Always Auto).” Item 9 was motivated by Martinho Fernandes' blog post of 27 May 2012, “ Handling dependent names.” The Item 12example demonstrating overloading on reference qualifiers is based on Casey's answer to the question, “ What's a use case for overloading member functions on referencequalifiers?,” posted to Stack Overflow on 14 January 2014. My Item 15treatment of C++14's expanded support for constexpr
functions incorporates information I received from Rein Halbersma. Item 16 is based on Herb Sutter's C++ and Beyond 2012 presentation, “You don't know const
and mutable
.” Item 18's advice to have factory functions return std::unique_ptr
s is based on Herb Sutter's 30 May 2013 article, “ GotW# 90 Solution: Factories.” In Item 19, fastLoadWidget
is derived from Herb Sutter's Going Native 2013 presentation, “ My Favorite C++ 10-Liner.” My treatment of std::unique_ptr
and incomplete types in Item 22draws on Herb Sutter's 27 November 2011 article, “ GotW #100: Compilation Firewalls” as well as Howard Hinnant's 22 May 2011 answer to the Stack Overflow question, “ Is std::unique_ptr
required to know the full definition of T?” The Matrix
addition example in Item 25is based on writings by David Abrahams. JoeArgonne's 8 December 2012 comment on the 30 November 2012 blog post, “ Another alternative to lambda move capture,” was the source of Item 32's std::bind
-based approach to emulating init capture in C++11. Item 37's explanation of the problem with an implicit detach in std::thread
's destructor is taken from Hans-J. Boehm's 4 December 2008 paper, “ N2802: A plea to reconsider detach-on-destruction for thread objects.” Item 41was originally motivated by discussions of David Abrahams' 15 August 2009 blog post, “ Want speed? Pass by value.” The idea that move-only types deserve special treatment is due to Matthew Fioravante, while the analysis of assignment-based copying stems from comments by Howard Hinnant. In Item 42, Stephan T. Lavavej and Howard Hinnant helped me understand the relative performance profiles of emplacement and insertion functions, and Michael Winterberg brought to my attention how emplacement can lead to resource leaks. (Michael credits Sean Parent's Going Native 2013 presentation, “ C++ Seasoning,” as his source). Michael also pointed out how emplacement functions use direct initialization, while insertion functions use copy initialization.
Reviewing drafts of a technical book is a demanding, time-consuming, and utterly critical task, and I'm fortunate that so many people were willing to do it for me. Full or partial drafts of Effective Modern C++ were officially reviewed by Cassio Neri, Nate Kohl, Gerhard Kreuzer, Leor Zolman, Bart Vandewoestyne, Stephan T. Lavavej, Nevin “:-)” Liber, Rachel Cheng, Rob Stewart, Bob Steagall, Damien Watkins, Bradley E. Needham, Rainer Grimm, Fredrik Winkler, Jonathan Wakely, Herb Sutter, Andrei Alexandrescu, Eric Niebler, Thomas Becker, Roger Orr, Anthony Williams, Michael Winterberg, Benjamin Huchley, Tom Kirby-Green, Alexey A Nikitin, William Dealtry, Hubert Matthews, and Tomasz Kaminski. I also received feedback from several readers through O'Reilly's Early Release EBooksand Safari Books Online's Rough Cuts, comments on my blog ( The View from Aristeia ), and email. I'm grateful to each of these people. The book is much better than it would have been without their help. I'm particularly indebted to Stephan T. Lavavej and Rob Stewart, whose extraordinarily detailed and comprehensive remarks lead me to worry that they spent nearly as much time on this book as I did. Special thanks also go to Leor Zolman, who, in addition to reviwing the manuscript, double-checked all the code examples.
Dedicated reviews of digital versions of the book were performed by Gerhard Kreuzer, Emyr Williams, and Bradley E. Needham.
My decision to limit the line length in code displays to 64 characters (the maximum likely to display properly in print as well as across a variety of digital devices, device orientations, and font configurations) was based on data provided by Michael Maher.
Ashley Morgan Williams made dining at the Lake Oswego Pizzicato uniquely entertaining. When it comes to man-sized Caesars, she's the go-to gal.
More than 20 years after first living through my playing author, my wife, Nancy L. Urbano, once again tolerated many months of distracted conversations with a cocktail of resignation, exasperation, and timely splashes of understanding and support. During the same period, our dog, Darla, was largely content to doze away the hours I spent staring at computer screens, but she never let me forget that there's life beyond the keyboard.
If you're an experienced C++ programmer and are anything like me, you initially approached C++11 thinking, “Yes, yes, I get it. It's C++, only more so.” But as you learned more, you were surprised by the scope of the changes. auto
declarations, range-based for loops, lambda expressions, and rvalue references change the face of C++, to say nothing of the new concurrency features. And then there are the idiomatic changes. 0
and typedef
s are out, nullptr
and alias declarations are in. Enums should now be scoped. Smart pointers are now preferable to built-in ones. Moving objects is normally better than copying them.
There's a lot to learn about C++11, not to mention C++14.
More importantly, there's a lot to learn about making effective use of the new capabilities. If you need basic information about "modern" C++ features, resources abound, but if you're looking for guidance on how to employ the features to create software that's correct, efficient, maintainable, and portable, the search is more challenging. That's where this book comes in. It's devoted not to describing the features of C++11 and C++14, but instead to their effective application.
The information in the book is broken into guidelines called Items . Want to understand the various forms of type deduction? Or know when (and when not) to use auto
declarations? Are you interested in why const
member functions should be thread safe, how to implement the Pimpl Idiom using std::unique_ptr
, why you should avoid default capture modes in lambda expressions, or the differences between std::atomic
and volatile
? The answers are all here. Furthermore, they're platform-independent, Standards-conformant answers. This is a book about portable C++.
Читать дальше