You are previewing Effective Modern C++.
O'Reilly logo
Effective Modern C++

Book Description

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. using modern C++.

Table of Contents

  1. Copyright/Revision History
  2. From the Publisher
    1. Using Code Examples
    2. Safari® Books Online
    3. How to Contact Us
  3. Acknowledgments
  4. Introduction
    1. Terminology and Conventions
    2. Reporting Bugs and Suggesting Improvements
  5. 1. Deducing Types
    1. Item 1: Understand template type deduction.
      1. <span xmlns="" xmlns:epub="" class="heading-label">Case 1:</span> <em xmlns="" xmlns:epub=""><code>ParamType</code></em> is a Reference or Pointer, but not a Universal Reference is a Reference or Pointer, but not a Universal Reference
      2. Case 2: <em xmlns="" xmlns:epub=""><code>ParamType</code></em> is a Universal Reference is a Universal Reference
      3. Case 3: <em xmlns="" xmlns:epub=""><code>ParamType</code></em> is Neither a Pointer nor a Reference is Neither a Pointer nor a Reference
      4. Array Arguments
      5. Function Arguments
    2. Item 2: Understand <code xmlns="" xmlns:epub="">auto</code> type deduction. type deduction.
    3. Item 3: Understand <code xmlns="" xmlns:epub="">decltype</code>..
    4. Item 4: Know how to view deduced types.
      1. IDE Editors
      2. Compiler Diagnostics
      3. Runtime Output
  6. 2. <code xmlns="" xmlns:epub="">auto</code>
    1. Item 5: Prefer <code xmlns="" xmlns:epub="">auto</code> to explicit type declarations. to explicit type declarations.
    2. Item 6: Use the explicitly typed initializer idiom when <code xmlns="" xmlns:epub="">auto</code> deduces undesired types. deduces undesired types.
  7. 3. Moving to Modern C++
    1. Item 7: Distinguish between <code xmlns="" xmlns:epub="">()</code> and and <code xmlns="" xmlns:epub="">{}</code> when creating objects. when creating objects.
    2. Item 8:<span xmlns="" xmlns:epub="" class="margin-left: -25pt; color: green;">&#8194;Prefer</span> <code xmlns="" xmlns:epub="">nullptr</code> to to <code xmlns="" xmlns:epub="">0</code> and and <code xmlns="" xmlns:epub="">NULL</code>..
    3. Item 9: Prefer alias declarations to <code xmlns="" xmlns:epub="">typedef</code>s.s.
    4. Item 10: Prefer scoped <code xmlns="" xmlns:epub="">enum</code>s to unscoped s to unscoped <code xmlns="" xmlns:epub="">enum</code>s.s.
    5. Item 11: Prefer deleted functions to private undefined ones.
    6. Item 12: Declare overriding functions <code xmlns="" xmlns:epub="">override</code>..
    7. Item 13: Prefer <code xmlns="" xmlns:epub="">const_iterator</code>s to s to <code xmlns="" xmlns:epub="">iterator</code>s.s.
    8. Item 14: Declare functions <code xmlns="" xmlns:epub="">noexcept</code> if they won&#8217;t emit exceptions. if they won’t emit exceptions.
    9. Item 15: Use <code xmlns="" xmlns:epub="">constexpr</code> whenever possible. whenever possible.
    10. Item 16: Make <code xmlns="" xmlns:epub="">const</code> member functions thread safe. member functions thread safe.
    11. Item 17: Understand special member function generation.
  8. 4. Smart Pointers
    1. Item 18: Use <code xmlns="" xmlns:epub="">std::unique_ptr</code> for exclusive-ownership resource management. for exclusive-ownership resource management.
    2. Item 19: Use <code xmlns="" xmlns:epub="">std::shared_ptr</code> for shared-ownership resource management. for shared-ownership resource management.
    3. Item 20: Use <code xmlns="" xmlns:epub="">std::weak_ptr</code> for for <code xmlns="" xmlns:epub="">std::shared_ptr</code>-like pointers that can dangle.-like pointers that can dangle.
    4. Item 21: Prefer <code xmlns="" xmlns:epub="">std::make_unique</code> and and <code xmlns="" xmlns:epub="">std::make_shared</code> to direct use of to direct use of <code xmlns="" xmlns:epub="">new</code>..
    5. Item 22: When using the Pimpl Idiom, define special member functions in the implementation file.
  9. 5. Rvalue References, Move Semantics, <span xmlns="" xmlns:epub="" class="keep-together">and Perfect Forwarding</span>
    1. Item 23: Understand <code xmlns="" xmlns:epub="">std::move</code> and and <span xmlns="" xmlns:epub="" class="keep-together"><code>std::forward</code>.</span>
    2. Item 24: Distinguish universal references from rvalue references.
    3. Item 25: Use <code xmlns="" xmlns:epub="">std::move</code> on rvalue references, on rvalue references, <code xmlns="" xmlns:epub="">std::forward</code> on universal references. on universal references.
    4. Item 26: Avoid overloading on universal references.
    5. Item 27: Familiarize yourself with alternatives to overloading on universal references.
      1. Abandon overloading
      2. Pass by const T&
      3. Pass by value
      4. Use Tag dispatch
      5. Constraining templates that take universal references
      6. Trade-offs
    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.
      1. Braced initializers
      2. <code xmlns="" xmlns:epub="">0</code> or or <code xmlns="" xmlns:epub="">NULL</code> as null pointers as null pointers
      3. Declaration-only integral <code xmlns="" xmlns:epub="">static</code> <code xmlns="" xmlns:epub="">const</code> and and <code xmlns="" xmlns:epub="">constexpr</code> data members data members
      4. Overloaded function names and template names
      5. Bitfields
      6. Upshot
  10. 6. Lambda Expressions
    1. Item 31: Avoid default capture modes.
    2. Item 32: Use init capture to move objects into closures.
    3. Item 33: Use <code xmlns="" xmlns:epub="">decltype</code> on on <code xmlns="" xmlns:epub="">auto&amp;&amp;</code> parameters to parameters to <code xmlns="" xmlns:epub="">std::forward</code> them. them.
    4. Item 34: Prefer lambdas to <code xmlns="" xmlns:epub="">std::bind</code>..
  11. 7. The Concurrency API
    1. Item 35: Prefer task-based programming to thread-based.
    2. Item 36: Specify <code xmlns="" xmlns:epub="">std::launch::async</code> if asynchronicity is essential. if asynchronicity is essential.
    3. Item 37: Make <code xmlns="" xmlns:epub="">std::threads</code> unjoinable on all paths. unjoinable on all paths.
    4. Item 38: Be aware of varying thread handle destructor behavior.
    5. Item 39: Consider <code xmlns="" xmlns:epub="">void</code> futures for one-shot event communication. futures for one-shot event communication.
    6. Item 40: Use <code xmlns="" xmlns:epub="">std::atomic</code> for concurrency, for concurrency, <span xmlns="" xmlns:epub="" class="keep-together"><code>volatile</code></span> for special memory. for special memory.
  12. 8. 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.
  13. Index