Share E-Book
Scan to open this page

Scan with your phone to open this page

Author: Scott Meyers

Rating No ratings yet

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++. At first glance, C++11 and C++14 are defined by the new features they introduce, e.g., auto type declarations, move semantics, lambda expressions, and concurrency support. Information on these features is easy to come by, but learning to apply them effectively (such that the resulting software is correct, efficient, maintainable, and portable) is more challenging. That’s the role of this book. It describes how to write effective software using C++11 and C++14, i.e., using modern C++. Topics include: The pros and cons of uniform initialization, noexcept specifications, perfect forwarding, and smart pointer make functions. The relationships among std::move, std::forward, rvalues references, and universal references. The most effective forms of lambda capture. How best practices in “old” C++ programming (i.e., C++98) require revision for modern C++. Effective Modern C++ follows the proven format of Scott Meyers’ earlier Effective books (Effective C++, More Effective C++, and Effective STL), but covers entirely new material. It’s essential reading for every modern C++ software developer.

AI Reading Assistant

Whole-book reading guide from stratified index samples; jump to passages in the text

AI guide
【One-Line Pitch】 A practical, item-based guide to writing correct, efficient, and maintainable code with C++11 and C++14, ideal for experienced C++ developers transitioning from C++98 or seeking to master modern idioms like move semantics, type deduction, and smart pointers. 【Book Arc】 - **Opening (~0%–9%)**: Introduces the book's scope and terminology (e.g., raw vs. smart pointers, signatures, undefined behavior), then dives into template type deduction rules, distinguishing cases for references, universal references, and by-value parameters. - **Early (~9%–25%)**: Explores `auto` and `decltype` type deduction, including pitfalls like braced initializers and how to use `decltype(auto)` for perfect forwarding in function templates. - **Early (~25%–34%)**: Covers variable declaration best practices—preferring `auto` over explicit types, avoiding `std::function` for closures (due to heap allocation), and understanding proxy classes like `std::vector<bool>::reference`. - **Middle (~34%–47%)**: Discusses initialization syntax (braces vs. parentheses), the most vexing parse, and `std::initializer_list` overload traps; then shifts to null pointer best practices (`nullptr` over `0`/`NULL`) and scoped enums (`enum class`) for type safety. - **Late (~47%–end)**: Continues with advanced topics like `noexcept`, smart pointer `make` functions, and universal references, though excerpts thin out; the book closes with practical guidance on applying these idioms consistently. 【Key Takeaways】 - **Template type deduction has three cases** (Early): reference/pointer (non-universal), universal reference, and by-value; each has distinct rules (e.g., references are ignored, const is dropped for by-value). This is foundational for writing generic code. - **`auto` type deduction differs from template deduction** (Early): `auto` treats braced initializers as `std::initializer_list`, while templates don't; C++14 `auto` return types and lambda params use template rules. This prevents subtle bugs in initialization. - **`decltype` gives exact types, enabling `decltype(auto)`** (Early): Use it for perfect-forwarding function returns, like `authAndAccess`, to preserve references and constness; C++11 requires trailing return types. - **Prefer `auto` over explicit types for variables** (Early): It avoids uninitialized variables and type mismatches, but beware proxy classes like `std::vector<bool>::reference` that can dangle; use `static_cast` to materialize values. - **Braced initialization is "uniform" but has traps** (Middle): It prevents narrowing and vexing parse, but can hijack overloads via `std::initializer_list` (e.g., `std::vector<int>{10, 20}` creates 2 elements, not 10). Design constructors to avoid surprises. - **Use `nullptr` instead of `0` or `NULL`** (Middle): In templates, `0` and `NULL` deduce as integral types, causing type errors; `nullptr` converts cleanly to any pointer type, avoiding overload ambiguity. - **Prefer scoped enums (`enum class`)** (Middle): They prevent namespace pollution and implicit conversions to integral/floating types, catching errors like comparing a color to a double; use `std::underlying_type` for generic access. 【Reading Tips】 - **Deep-read Items 1–4** (type deduction) and Item 7 (initialization) carefully—they underpin most modern C++ pitfalls; skim the Boost.TypeIndex example in Item 4 if you're not using Boost. - **Skim the "Things to Remember" summaries** at each item's end for quick reference; they distill the key rules without the detailed rationale. - **Watch for cross-references** (e.g., Item 24 on universal references, Item 25 on `std::forward`); read them in sequence if you're new to move semantics, as they build on each other. - **Practice with the `authAndAccess` example** (Item 3) to solidify `decltype(auto)` and perfect forwarding; it's a canonical pattern for generic container access. - **Take away the "why" behind each rule** (e.g., why `auto` beats `std::function` for closures) to apply these idioms beyond the book's examples. 【Coverage Limits】 Excerpts cover roughly the first half of the book (Items 1–10); later items on smart pointers, `noexcept`, and concurrency are mentioned but not detailed here.
Page 1
mantics, lambda expressions, and concurrency support). The challenge is learning to use those features effectively—so that your how to use C++ in software ...
View in text
Excerpt 2
r implies template type deduction, not auto type deduction. Item 3: Understand decltype. decltype is an odd creature. Given a name or an expression, decltype...
View in text
Excerpt 3
sion that operator[] for std::vector<bool> returns a refer‐ ence to a bit, for example, and the Standard Library’s smart pointer types (see Chap‐ ter 4) are ...
View in text
Excerpt 4
space pollution offered by scoped enums is reason enough to prefer them over their unscoped siblings, but scoped enums have a second compelling advantage: th...
View in text
Excerpt 5
or of a function is of key interest to clients. Callers can query a function’s noexcept status, and the results of such a query can affect the exception safe...
View in text
Excerpt 6
are remarkably varied. About the only functionality common to all is default construction. Because comprehensive references for these APIs are widely availab...
View in text
Excerpt 7
using std::shared_ptrs everywhere, how can this code leak? The answer has to do with compilers’ translation of source code into object code. At runtime, the ...
View in text
Excerpt 8
move unconditionally casts its argument to an rvalue, while std::forward performs this cast only if a particular condition is fulfilled. That’s it. The expla...
View in text
Tags
AI categories
C++Programming LanguageCode
ISBN: 1491903996
Publish Year: 2014
Language: English
Pages: 320
File Format: PDF
File Size: 4.5 MB
Text Preview (First 20 pages)
Registered users can read the full content for free

Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.

Generating text preview…