Effective Modern C++

42 Specific Ways to Improve Your Use of C++11 and C++14

Publisher: O’Reilly Media
Final Release Date: November 2014
Pages: 336
42 Specific Ways to Improve Your Use of C++11 and C++14

Coming to grips with C++11 and C++14 is more than a matter of familiarizing yourself with the features they introduce (e.g., auto type declarations, move semantics, lambda expressions, and concurrency support). The challenge is learning to use those features effectively—so that your software is correct, efficient, maintainable, and portable. That’s where this practical book comes in. It describes how to write truly great software using C++11 and C++14—i.e. usingmodern C++. Topics include:

  • The pros and cons of braced initialization, noexcept specifications, perfect forwarding, and smart pointer make functions
  • The relationships among std::move, std::forward, rvalue references, and universal references
  • Techniques for writing clear, correct, effective lambda expressions
  • How std::atomic differs from volatile, how each should be used, and how they relate to C++’s concurrency API
  • How best practices in “old” C++ programming (i.e., C++98) require revision for software development in modern C++

Effective Modern C++ follows the proven guideline-based, example-driven format of Scott Meyers’ earlier books, but covers entirely new material.

About the Author

Scott Meyers

For more than 20 years, Scott Meyers’ Effective C++ books (Effective C++, More Effective C++, and Effective STL) have set the bar for C++ programming guidance. His clear, engaging explanations of complex technical material have earned him a worldwide following, and they keep him in demand as a trainer, consultant, and conference presenter. Winner of the 2009 Dr. Dobb’s Excellence in Programming Award, he has a Ph.D. in Computer Science from Brown University. His web site is aristeia.com.

View Scott Meyers’s full profile page.

Colophon

The animal on the cover of Effective Modern C++ is a Rose-crowned fruit dove (Ptilinopus regina). This species of dove also goes by the names pink-capped fruit dove or Swainson’s fruit dove. It is distinguished by its striking plumage: grey head and breast, orange belly, whitish throat, yellow-orange iris, and grey green bill and feet. Distributed in lowland rainforests in eastern Australia, monsoon forests in northern Australia, and the Lesser Sunda Islands and Maluku Islands of Indonesia, the Rose-crowned fruit dove’s diet consists of various fruits like figs (which it swallows whole), palms, and vines. Camphor Laurel, a large evergreen tree, is another food source for the fruit dove. They feed—in pairs, small parties, or singly—in rainforest canopies, usually in the morning or late afternoon. To hydrate, they get water from leaves or dew, not from the ground. The fruit dove is considered vulnerable in New South Wales due to rainforest clearing and fragmentation, logging, weeds, fire regime–altered habitats, and the removal of Laurel Camphor without adequate alternatives. Many of the animals on O’Reilly covers are endangered; all of them are important to the world. To learn more about how you can help, go to animals.oreilly.com. The cover image is from Wood’s Illustrated Natural History, bird volume. The cover fonts are URW Typewriter and Guardian Sans. The text font is Adobe Minion Pro; the heading font is Adobe Myriad Condensed; and the code font is Dalton Maag’s Ubuntu Mono.

Table of Contents

  1. Chapter 1 Deducing Types
    1. Item 1: Understand template type deduction.
    2. Item 2: Understand auto type deduction.
    3. Item 3: Understand decltype.
    4. Item 4: Know how to view deduced types.
  2. Chapter 2 auto
    1. Item 5: Prefer auto to explicit type declarations.
    2. Item 6: Use the explicitly typed initializer idiom when auto deduces undesired types.
  3. Chapter 3 Moving to Modern C++
    1. Item 7: Distinguish between () and {} when creating objects.
    2. Item 8: Prefer nullptr to 0 and NULL.
    3. Item 9: Prefer alias declarations to typedefs.
    4. Item 10: Prefer scoped enums to unscoped enums.
    5. Item 11: Prefer deleted functions to private undefined ones.
    6. Item 12: Declare overriding functions override.
    7. Item 13: Prefer const_iterators to iterators.
    8. Item 14: Declare functions noexcept if they won’t emit exceptions.
    9. Item 15: Use constexpr whenever possible.
    10. Item 16: Make const member functions thread safe.
    11. Item 17: Understand special member function generation.
  4. Chapter 4 Smart Pointers
    1. Item 18: Use std::unique_ptr for exclusive-ownership resource management.
    2. Item 19: Use std::shared_ptr for shared-ownership resource management.
    3. Item 20: Use std::weak_ptr for std::shared_ptr-like pointers that can dangle.
    4. Item 21: Prefer std::make_unique and std::make_shared to direct use of new.
    5. Item 22: When using the Pimpl Idiom, define special member functions in the implementation file.
  5. Chapter 5 Rvalue References, Move Semantics, and Perfect Forwarding
    1. Item 23: Understand std::move and std::forward.
    2. Item 24: Distinguish universal references from rvalue references.
    3. Item 25: Use std::move on rvalue references, std::forward on universal references.
    4. Item 26: Avoid overloading on universal references.
    5. Item 27: Familiarize yourself with alternatives to overloading on universal references.
    6. Item 28: Understand reference collapsing.
    7. Item 29: Assume that move operations are not present, not cheap, and not used.
    8. Item 30: Familiarize yourself with perfect forwarding failure cases.
  6. Chapter 7 Lambda Expressions
    1. Item 31: Avoid default capture modes.
    2. Item 32: Use init capture to move objects into closures.
    3. Item 33: Use decltype on auto&& parameters to std::forward them.
    4. Item 34: Prefer lambdas to std::bind.
  7. Chapter 8 The Concurrency API
    1. Item 35: Prefer task-based programming to thread-based.
    2. Item 36: Specify std::launch::async if asynchronicity is essential.
    3. Item 37: Make std::threads unjoinable on all paths.
    4. Item 38: Be aware of varying thread handle destructor behavior.
    5. Item 39: Consider void futures for one-shot event communication.
    6. Item 40: Use std::atomic for concurrency, volatile for special memory.
  8. Chapter 9 Tweaks
    1. Item 41: Consider pass by value for copyable parameters that are cheap to move and always copied.
    2. Item 42: Consider emplacement instead of insertion.
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s