Discovering Modern C++: An Intensive Course for Scientists, Engineers, and Programmers

Book description

As scientific and engineering projects grow larger and more complex, it is increasingly likely that those projects will be written in C++. With embedded hardware growing more powerful, much of its software is moving to C++, too. Mastering C++ gives you strong skills for programming at nearly every level, from “close to the hardware” to the highest-level abstractions. In short, C++ is a language that scientific and technical practitioners need to know.

Peter Gottschling’s Discovering Modern C++ is an intensive introduction that guides you smoothly to sophisticated approaches based on advanced features. Gottschling introduces key concepts using examples from many technical problem domains, drawing on his extensive experience training professionals and teaching C++ to students of physics, math, and engineering.

This book is designed to help you get started rapidly and then master increasingly robust features, from lambdas to expression templates. You’ll also learn how to take advantage of the powerful libraries available to C++ programmers: both the Standard Template Library (STL) and scientific libraries for arithmetic, linear algebra, differential equations, and graphs.

Throughout, Gottschling demonstrates how to write clear and expressive software using object orientation, generics, metaprogramming, and procedural techniques. By the time you’re finished, you’ll have mastered all the abstractions you need to write C++ programs with exceptional quality and performance.

Table of contents

  1. About This E-Book
  2. Title Page
  3. Copyright Page
  4. Dedication Page
  5. Contents
  6. Preface
    1. Reasons to Learn C++
    2. Reasons to Read This Book
    3. The Beauty and the Beast
    4. Languages in Science and Engineering
    5. Typographical Conventions
  7. Acknowledgments
  8. About the Author
  9. Chapter 1. C++ Basics
    1. 1.1 Our First Program
    2. 1.2 Variables
      1. 1.2.1 Constants
      2. 1.2.2 Literals
      3. 1.2.3 Non-narrowing Initialization
      4. 1.2.4 Scopes
    3. 1.3 Operators
      1. 1.3.1 Arithmetic Operators
      2. 1.3.2 Boolean Operators
      3. 1.3.3 Bitwise Operators
      4. 1.3.4 Assignment
      5. 1.3.5 Program Flow
      6. 1.3.6 Memory Handling
      7. 1.3.7 Access Operators
      8. 1.3.8 Type Handling
      9. 1.3.9 Error Handling
      10. 1.3.10 Overloading
      11. 1.3.11 Operator Precedence
      12. 1.3.12 Avoid Side Effects!
    4. 1.4 Expressions and Statements
      1. 1.4.1 Expressions
      2. 1.4.2 Statements
      3. 1.4.3 Branching
      4. 1.4.4 Loops
      5. 1.4.5 goto
    5. 1.5 Functions
      1. 1.5.1 Arguments
      2. 1.5.2 Returning Results
      3. 1.5.3 Inlining
      4. 1.5.4 Overloading
      5. 1.5.5 main Function
    6. 1.6 Error Handling
      1. 1.6.1 Assertions
      2. 1.6.2 Exceptions
      3. 1.6.3 Static Assertions
    7. 1.7 I/O
      1. 1.7.1 Standard Output
      2. 1.7.2 Standard Input
      3. 1.7.3 Input/Output with Files
      4. 1.7.4 Generic Stream Concept
      5. 1.7.5 Formatting
      6. 1.7.6 Dealing with I/O Errors
    8. 1.8 Arrays, Pointers, and References
      1. 1.8.1 Arrays
      2. 1.8.2 Pointers
      3. 1.8.3 Smart Pointers
      4. 1.8.4 References
      5. 1.8.5 Comparison between Pointers and References
      6. 1.8.6 Do Not Refer to Outdated Data!
      7. 1.8.7 Containers for Arrays
    9. 1.9 Structuring Software Projects
      1. 1.9.1 Comments
      2. 1.9.2 Preprocessor Directives
    10. 1.10 Exercises
      1. 1.10.1 Age
      2. 1.10.2 Arrays and Pointers
      3. 1.10.3 Read the Header of a Matrix Market File
  10. Chapter 2. Classes
    1. 2.1 Program for Universal Meaning Not for Technical Details
    2. 2.2 Members
      1. 2.2.1 Member Variables
      2. 2.2.2 Accessibility
      3. 2.2.3 Access Operators
      4. 2.2.4 The Static Declarator for Classes
      5. 2.2.5 Member Functions
    3. 2.3 Setting Values: Constructors and Assignments
      1. 2.3.1 Constructors
      2. 2.3.2 Assignment
      3. 2.3.3 Initializer Lists
      4. 2.3.4 Uniform Initialization
      5. 2.3.5 Move Semantics
    4. 2.4 Destructors
      1. 2.4.1 Implementation Rules
      2. 2.4.2 Dealing with Resources Properly
    5. 2.5 Method Generation Résumé
    6. 2.6 Accessing Member Variables
      1. 2.6.1 Access Functions
      2. 2.6.2 Subscript Operator
      3. 2.6.3 Constant Member Functions
      4. 2.6.4 Reference-Qualified Members
    7. 2.7 Operator Overloading Design
      1. 2.7.1 Be Consistent!
      2. 2.7.2 Respect the Priority
      3. 2.7.3 Member or Free Function
    8. 2.8 Exercises
      1. 2.8.1 Polynomial
      2. 2.8.2 Move Assignment
      3. 2.8.3 Initializer List
      4. 2.8.4 Resource Rescue
  11. Chapter 3. Generic Programming
    1. 3.1 Function Templates
      1. 3.1.1 Instantiation
      2. 3.1.2 Parameter Type Deduction
      3. 3.1.3 Dealing with Errors in Templates
      4. 3.1.4 Mixing Types
      5. 3.1.5 Uniform Initialization
      6. 3.1.6 Automatic return Type
    2. 3.2 Namespaces and Function Lookup
      1. 3.2.1 Namespaces
      2. 3.2.2 Argument-Dependent Lookup
      3. 3.2.3 Namespace Qualification or ADL
    3. 3.3 Class Templates
      1. 3.3.1 A Container Example
      2. 3.3.2 Designing Uniform Class and Function Interfaces
    4. 3.4 Type Deduction and Definition
      1. 3.4.1 Automatic Variable Type
      2. 3.4.2 Type of an Expression
      3. 3.4.3 decltype(auto)
      4. 3.4.4 Defining Types
    5. 3.5 A Bit of Theory on Templates: Concepts
    6. 3.6 Template Specialization
      1. 3.6.1 Specializing a Class for One Type
      2. 3.6.2 Specializing and Overloading Functions
      3. 3.6.3 Partial Specialization
      4. 3.6.4 Partially Specializing Functions
    7. 3.7 Non-Type Parameters for Templates
    8. 3.8 Functors
      1. 3.8.1 Function-like Parameters
      2. 3.8.2 Composing Functors
      3. 3.8.3 Recursion
      4. 3.8.4 Generic Reduction
    9. 3.9 Lambda
      1. 3.9.1 Capture
      2. 3.9.2 Capture by Value
      3. 3.9.3 Capture by Reference
      4. 3.9.4 Generalized Capture
      5. 3.9.5 Generic Lambdas
    10. 3.10 Variadic Templates
    11. 3.11 Exercises
      1. 3.11.1 String Representation
      2. 3.11.2 String Representation of Tuples
      3. 3.11.3 Generic Stack
      4. 3.11.4 Iterator of a Vector
      5. 3.11.5 Odd Iterator
      6. 3.11.6 Odd Range
      7. 3.11.7 Stack of bool
      8. 3.11.8 Stack with Custom Size
      9. 3.11.9 Deducing Non-type Template Arguments
      10. 3.11.10 Trapezoid Rule
      11. 3.11.11 Functor
      12. 3.11.12 Lambda
      13. 3.11.13 Implement make_unique
  12. Chapter 4. Libraries
    1. 4.1 Standard Template Library
      1. 4.1.1 Introductory Example
      2. 4.1.2 Iterators
      3. 4.1.3 Containers
      4. 4.1.4 Algorithms
      5. 4.1.5 Beyond Iterators
    2. 4.2 Numerics
      1. 4.2.1 Complex Numbers
      2. 4.2.2 Random Number Generators
    3. 4.3 Meta-programming
      1. 4.3.1 Limits
      2. 4.3.2 Type Traits
    4. 4.4 Utilities
      1. 4.4.1 Tuple
      2. 4.4.2 function
      3. 4.4.3 Reference Wrapper
    5. 4.5 The Time Is Now
    6. 4.6 Concurrency
    7. 4.7 Scientific Libraries Beyond the Standard
      1. 4.7.1 Other Arithmetics
      2. 4.7.2 Interval Arithmetic
      3. 4.7.3 Linear Algebra
      4. 4.7.4 Ordinary Differential Equations
      5. 4.7.5 Partial Differential Equations
      6. 4.7.6 Graph Algorithms
    8. 4.8 Exercises
      1. 4.8.1 Sorting by Magnitude
      2. 4.8.2 STL Container
      3. 4.8.3 Complex Numbers
  13. Chapter 5. Meta-Programming
    1. 5.1 Let the Compiler Compute
      1. 5.1.1 Compile-Time Functions
      2. 5.1.2 Extended Compile-Time Functions
      3. 5.1.3 Primeness
      4. 5.1.4 How Constant Are Our Constants?
    2. 5.2 Providing and Using Type Information
      1. 5.2.1 Type Traits
      2. 5.2.2 Conditional Exception Handling
      3. 5.2.3 A const-Clean View Example
      4. 5.2.4 Standard Type Traits
      5. 5.2.5 Domain-Specific Type Properties
      6. 5.2.6 enable_if
      7. 5.2.7 Variadic Templates Revised
    3. 5.3 Expression Templates
      1. 5.3.1 Simple Operator Implementation
      2. 5.3.2 An Expression Template Class
      3. 5.3.3 Generic Expression Templates
    4. 5.4 Meta-Tuning: Write Your Own Compiler Optimization
      1. 5.4.1 Classical Fixed-Size Unrolling
      2. 5.4.2 Nested Unrolling
      3. 5.4.3 Dynamic Unrolling–Warm-up
      4. 5.4.4 Unrolling Vector Expressions
      5. 5.4.5 Tuning an Expression Template
      6. 5.4.6 Tuning Reduction Operations
      7. 5.4.7 Tuning Nested Loops
      8. 5.4.8 Tuning Résumé
    5. 5.5 Exercises
      1. 5.5.1 Type Traits
      2. 5.5.2 Fibonacci Sequence
      3. 5.5.3 Meta-Program for Greatest Common Divisor
      4. 5.5.4 Vector Expression Template
      5. 5.5.5 Meta-List
  14. Chapter 6. Object-Oriented Programming
    1. 6.1 Basic Principles
      1. 6.1.1 Base and Derived Classes
      2. 6.1.2 Inheriting Constructors
      3. 6.1.3 Virtual Functions and Polymorphic Classes
      4. 6.1.4 Functors via Inheritance
    2. 6.2 Removing Redundancy
    3. 6.3 Multiple Inheritance
      1. 6.3.1 Multiple Parents
      2. 6.3.2 Common Grandparents
    4. 6.4 Dynamic Selection by Sub-typing
    5. 6.5 Conversion
      1. 6.5.1 Casting between Base and Derived Classes
      2. 6.5.2 const-Cast
      3. 6.5.3 Reinterpretation Cast
      4. 6.5.4 Function-Style Conversion
      5. 6.5.5 Implicit Conversions
    6. 6.6 CRTP
      1. 6.6.1 A Simple Example
      2. 6.6.2 A Reusable Access Operator
    7. 6.7 Exercises
      1. 6.7.1 Non-redundant Diamond Shape
      2. 6.7.2 Inheritance Vector Class
      3. 6.7.3 Clone Function
  15. Chapter 7. Scientific Projects
    1. 7.1 Implementation of ODE Solvers
      1. 7.1.1 Ordinary Differential Equations
      2. 7.1.2 Runge-Kutta Algorithms
      3. 7.1.3 Generic Implementation
      4. 7.1.4 Outlook
    2. 7.2 Creating Projects
      1. 7.2.1 Build Process
      2. 7.2.2 Build Tools
      3. 7.2.3 Separate Compilation
    3. 7.3 Some Final Words
  16. Appendix A. Clumsy Stuff
    1. A.1 More Good and Bad Scientific Software
    2. A.2 Basics in Detail
      1. A.2.1 More about Qualifying Literals
      2. A.2.2 static Variables
      3. A.2.3 More about if
      4. A.2.4 Duff’s Device
      5. A.2.5 More about main
      6. A.2.6 Assertion or Exception?
      7. A.2.7 Binary I/O
      8. A.2.8 C-Style I/O
      9. A.2.9 Garbarge Collection
      10. A.2.10 Trouble with Macros
    3. A.3 Real-World Example: Matrix Inversion
    4. A.4 Class Details
      1. A.4.1 Pointer to Member
      2. A.4.2 More Initialization Examples
      3. A.4.3 Accessing Multi-dimensional Arrays
    5. A.5 Method Generation
      1. A.5.1 Controlling the Generation
      2. A.5.2 Generation Rules
      3. A.5.3 Pitfalls and Design Guides
    6. A.6 Template Details
      1. A.6.1 Uniform Initialization
      2. A.6.2 Which Function Is Called?
      3. A.6.3 Specializing for Specific Hardware
      4. A.6.4 Variadic Binary I/O
    7. A.7 Using std::vector in C++03
    8. A.8 Dynamic Selection in Old Style
    9. A.9 Meta-Programming Details
      1. A.9.1 First Meta-Program in History
      2. A.9.2 Meta-Functions
      3. A.9.3 Backward-Compatible Static Assertion
      4. A.9.4 Anonymous Type Parameters
      5. A.9.5 Benchmark Sources of Dynamic Unrolling
      6. A.9.6 Benchmark for Matrix Product
  17. Appendix B. Programming Tools
    1. B.1 gcc
    2. B.2 Debugging
      1. B.2.1 Text-Based Debugger
      2. B.2.2 Debugging with Graphical Interface: DDD
    3. B.3 Memory Analysis
    4. B.4 gnuplot
    5. B.5 Unix, Linux, and Mac OS
  18. Appendix C. Language Definitions
    1. C.1 Value Categories
    2. C.2 Operator Overview
    3. C.3 Conversion Rules
      1. C.3.1 Promotion
      2. C.3.2 Other Conversions
      3. C.3.3 Usual Arithmetic Conversions
      4. C.3.4 Narrowing
  19. Bibliography
  20. Index
  21. Code Snippets

Product information

  • Title: Discovering Modern C++: An Intensive Course for Scientists, Engineers, and Programmers
  • Author(s): Peter Gottschling
  • Release date: December 2015
  • Publisher(s): Addison-Wesley Professional
  • ISBN: 9780134383682