My book Modern C++ Programming Cookbook has been published

I am pleased to announce that my book on modern C++ programming has been published by PacktPub. The book is called Modern C++ Programming Cookbook and can be ordered at packtpub.com and Amazon. The ISBN of the book is 9781786465184. The complete table of contents is available below.

The front cover

The book is organized in recipes, much like a cookbook (therefore the name). These recipes are organized in sections that introduce you to the topic, list any necessary pre-requisites and then explain how to do something and how that works. Throughout 112 recipes, the book covers both language and library features from C++11, C++14 and C++17, including the libraries for strings, containers, algorithms, iterators, input/output, regular expressions, threads, filesystem, atomic operations, and utilities. Besides that, there is a chapter for patterns and idioms and one dedicated for testing frameworks, that covers everything you need to know to get started with Boost.Test, Google Test and Catch.

This book is intended for all C++ developers, regardless of their experience. The beginner and intermediate developers will benefit the most from the book in their attempt to become prolific with C++. Experienced C++ developers, on the other hand, will find a good reference for many C++11, C++14, and C++17 language and library features that may come in handy from time to time. However, the book requires prior basic knowledge of C++, such as functions, classes, templates, namespaces, macros, and others. If you are not familiar with C++ at all, you should first read an introductory book to familiarize yourself with the core aspects.

Although C++17 has not yet been ratified as an ISO standard, the final version that is up for the ballot is well defined. In my book I discuss most of the important language and library features that made it into C++17. The C++17 features discussed in the book are:

  • structured bindings
  • fold expressions
  • constexpr if
  • new attributes ([[fallthrough]], [[nodiscard]], [[maybe_unused]])
  • new type deduction rules for list initialization
  • range based for loops improvements
  • general form of lambda expressions
  • std::invoke() and std::apply()
  • static_assert changes
  • non-member container access functions std::data(), std::size(), and std::empty()
  • std::search() searchers (Boyer-Moore and Boyer-Moore-Horspool)
  • chrono changes (floor(), round(), ceil(), and abs())
  • std::any
  • std::optional
  • std::variant (2 recipes)
  • std::string_view
  • std::scoped_lock
  • filesystem library (5 recipes)
  • shared_ptr and unique_ptr changes

All the samples in the book have been tested with VC++ 2017 (where possible), GCC 7 and Clang 5.

If you do not have the latest versions of these compilers, you can try all the samples in the book with an online compiler. gcc and Clang are available at wandbox.org and VC++ is available at webcompiler.cloudapp.net.

All the language and library features discussed in the book are available with these versions of the mentioned compilers, except for a few exceptions for VC++.

At this time, the following features are still not supported in VC++:

  • structured bindings
  • fold expressions
  • constexpr if
  • searchers for std::search()

Table of contents

  1. Learning Modern Core Language Features
    • Using auto whenever possible
    • Creating type aliases and alias templates
    • Understanding uniform initialization
    • Understanding the various forms of non-static member initialization
    • Controlling and querying object alignment
    • Using scoped enumerations
    • Using override and final for virtual methods
    • Using range-based for loops to iterate on a range
    • Enabling range-based for loops for custom types
    • Using explicit constructors and conversion operators to avoid implicit conversion
    • Using unnamed namespaces instead of static globals
    • Using inline namespaces for symbol versioning
    • Using structured bindings to handle multi-return values
  2. Working with Numbers and Strings
    • Converting between numeric and string types
    • Limits and other properties of numeric types
    • Generating pseudo-random numbers
    • Initializing all bits of internal state of a pseudo-random number generator
    • Using raw string literals to avoid escaping characters
    • Creating cooked user-defined literals
    • Creating raw user-defined literals
    • Creating a library of string helpers
    • Verifying the format of a string using regular expressions
    • Parsing the content of a string using regular expressions
    • Replacing the content of a string using regular expressions
    • Using string_view instead of constant string references
  3. Exploring Functions
    • Defaulted and deleted functions
    • Using lambdas with standard algorithms
    • Using generic lambdas
    • Writing a recursive lambda
    • Writing a function template with a variable number of arguments
    • Using fold expressions to simplify variadic function templates
    • Implementing higher-order functions map and fold
    • Composing functions into a higher-order function
    • Uniformly invoking anything callable
  4. Preprocessor and Compilation
    • Conditionally compiling your source code
    • Using the indirection pattern for preprocessor stringification and concatenation
    • Performing compile-time assertion checks with static_assert
    • Conditionally compiling classes and functions with enable_if
    • Selecting branches at compile time with constexpr if
    • Providing metadata to the compiler with attributes
  5. Standard Library Containers, Algorithms, and Iterators
    • Using vector as a default container
    • Using bitset for fixed-size sequences of bits
    • Using vector for variable-size sequences of bits
    • Finding elements in a range
    • Sorting a range
    • Initializing a range
    • Using set operations on a range
    • Using iterators to insert new elements in a container
    • Writing your own random access iterator
    • Container access with non-member functions
  6. General Purpose Utilities
    • Expressing time intervals with chrono::duration
    • Measuring function execution time with a standard clock
    • Generating hash values for custom types
    • Using std::any to store any value
    • Using std::optional to store optional values
    • Using std::variant as a type-safe union
    • Visiting a std::variant
    • Registering a function to be called when a program exits normally
    • Using type traits to query properties of types
    • Writing your own type traits
    • Using std::conditional to choose between types
  7. Working with Files and Streams
    • Reading and writing raw data from/to binary files
    • Reading and writing objects from/to binary files
    • Using localized settings for streams
    • Using I/O manipulators to control the output of a stream
    • Using monetary I/O manipulators
    • Using time I/O manipulators
    • Working with filesystem paths
    • Creating, copying, and deleting files and directories
    • Removing content from a file
    • Checking the properties of an existing file or directory
    • Enumerating the content of a directory
    • Finding a file
  8. Leveraging Threading and Concurrency
    • Working with threads
    • Handling exceptions from thread functions
    • Synchronizing access to shared data with mutexes and locks
    • Avoiding using recursive mutexes
    • Sending notifications between threads
    • Using promises and futures to return values from threads
    • Executing functions asynchronously
    • Using atomic types
    • Implementing parallel map and fold with threads
    • Implementing parallel map and fold with tasks
  9. Robustness and Performance
    • Using exceptions for error handling
    • Using noexcept for functions that do not throw
    • Ensuring constant correctness for a program
    • Creating compile-time constant expressions
    • Performing correct type casts
    • Using unique_ptr to uniquely own a memory resource
    • Using shared_ptr to share a memory resource
    • Implementing move semantics
  10. Implementing Patterns and Idioms
    • Avoiding repetitive if…else statements in factory patterns
    • Implementing the pimpl idiom
    • Implementing the named parameter idiom
    • Separating interfaces from implementations with the non-virtual interface idiom
    • Handling friendship with the attorney-client idiom
    • Static polymorphism with the curiously recurring template pattern
    • Implementing a thread-safe singleton
  11. Exploring Testing Frameworks
    • Getting started with Boost.Test
    • Writing and invoking tests with Boost.Test
    • Asserting with Boost.Test
    • Using test fixtures with Boost.Test
    • Controlling output with Boost.Test
    • Getting started with Google Test
    • Writing and invoking tests with Google Test
    • Asserting with Google Test
    • Using test fixtures with Google Test
    • Controlling output with Google Test
    • Getting started with Catch
    • Writing and invoking tests with Catch
    • Asserting with Catch
    • Controlling output with Catch

Credits

It took about eight months to complete this book and I got a lot of help from several people that I would like to thank to. First of all, is the team at PacktPub; although there were more people involve that I actually am aware of, I would like to thank Anurag Ghogre, Subhalaxmi Nadar and Nitin Dasan for all the help they provided throughout this time and the work they put in the project, as well as the other people that were involved with this book. I also want to thank David Corbin, whom I know for many years as “The CPU Wizard”, for reviewing the book and providing valuable feedback that made the book better. And last, but not least, I want to thank my wife for putting up with me through the many days and nights that I worked on this project.

5 Replies to “My book Modern C++ Programming Cookbook has been published”

  1. Salut Marius, am cumparat cartea si e interesanta, desi unele capitole mi se par cam nepotrivite si uneori greoie de citit.
    O intrebare te rog: La capitolul cu “Raw literals” ce reprezinta acel “s” de la sfarsit?

    auto filename {R”(C:\Users\Marius\Documents\)”s};
    auto pattern {R”((\w+)=(\d+)$)”s};

    Mersi anticipat,
    Taw

  2. Am uitat sa zic, te-am intrebat asta pentru ca nu compileaza in VS2017. E ciudat sa dau copy paste unui cod care nu compileaza…

  3. operator ""s e un operator literal standard pentru crearea de literale std:basic_string. A fost introdus in C++14, este definit in namespace-ul std::string_literals si este suportat de mult timp in VC++. Te rog citeste la pagina 86, sectiunea There’s more din reteta Creating cooked user-defined literals. Pe de alta parte, am explicat clar in acest post care sunt singurele features discutate si exemplificate in carte care nu sunt sau nu erau la momentul publicarii suportate in VC++.

  4. Mersi de raspuns, am citit toata cartea si, din pacate, speram la mai mult din partea ei.

    Am cateva intrebari si nu stiu unde sa le adresez, de exemplu de ce a trebuit sa faci “ifile.close();” la pagina 293, pentru ca ifstream are RAII.

    Totodata capitolul “Conditionally compiling your source code” (era frumos sa fi dat si numere capitolelor) e de fapt cod C chior, personal nu vad rostul explicatiilor de C intr-o carte strict pentru C++ 11/14/17. Capitolul urmator, la fel, am cumparat o carte de C++ modern, ma astept sa gasesc chestii moderne, nu sa invat cum se face #ifdef sau cum se concateneaza un string in #define

    Capitolul cu “measuring execution time” mi se pare din nou absolut useless, orice developer ar fi cautat pe google “measuring execution time c++ 11” si primul link de pe stackoverflow e fix solutia ta.

    Felicitari pentru carte totusi, am ales sa o cumpar tocmai ca sa incurajez scriitorii romani, dar sper ca urmatoarea sa fie mai buna.

  5. Multumesc pentru feedback. Cartea ar trebuie sa aiba cate ceva pentru toata lumea. Tinta principala a cartii, asa cum e expicit mentionat in carte, sunt programatorii de C++ de nivel incepator si mediu, desi sper ca si cei avansati sa gaseasca cate ceva util (de exemplu sunt abordate toate lucrurile noi, importante din C++17). Prin urmare nu ma astept ca toata lumea sa gaseasca ceva nou in fiecare capitol sau reteta. Pe de alta parte cartea nu pretinde sa contina informatii in premiera sau unice. Desigur ca atunci cand cauti solutii la diverse probleme vei gasi aceleasi informatii sau acelasi timp de informatii pe net. Cred ca e valabil pentru orice carte in ziua de astazi. Dimpotriva, cartea e o coletie de retete pentru probleme des intalnite. Asta inseamna ca si solutiile pot fi gasite deja in alta parte. Ce am incercat eu a fost sa pun laolalta o colectie de retete utile si organizate intr-o forma care sa ajute pe cei care vor sa invete cu o carte in mana, nu dupa stackoverflow.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.