About the Book

C++11 was a major update for the language. With all the modern features like lambdas, constexpr, variadic templates, threading, range-based for loops, smart pointers and many more powerful elements, it signalled enormous progress for the language. Even now, in 2019, many teams struggle to modernise their projects to leverage all the modern features. Later there was a minor update - C++14, which improved some things from the previous Standard and added a few smaller elements.

Although C++17 is not as big as C++11, it’s larger than C++14 and brings many exciting additions and improvements. And this book will guide through all of them!

The book brings you exclusive content about C++17 and draws from the experience of many articles that have appeared at bfilipek.com. The material was rewritten from the ground-up and updated with the latest information. All of that equipped with lots of new examples and practical tips. Additionally, the book provides insight into the current implementation status, compiler support, performance issues and other relevant knowledge to boost your current projects.

Who This Book is For

This book is intended for all C++ developers who have at least essential experience with C++11/14.

The principal aim of the book is to equip you with practical knowledge about C++17. After reading the book, you’ll be able to move past C++11 and leverage the latest C++ techniques in your day to day tasks.

Please don’t worry if you’re not an expert in C++11/14. This book provides the necessary background, so you’ll get the information in a proper context.

Overall Structure of the Book

C++17 brings a lot of changes to the language and the Standard Library. In this book, all the new features were categorised into a few segments, so that they are easier to comprehend.

As a result, the book has the following sections:

  • Part One - C++17 Language Features
    • Fixes and Deprecation
    • Language Clarification
    • General Language Features
    • Templates
    • Attributes
  • Part Two - C++17 The Standard Library
    • std::optional
    • std::variant
    • std::any
    • std::string_view
    • String Operations
    • Filesystem
    • Parallel STL
    • Other Changes
  • Part Three - More Examples and Use Cases
    • Refactoring with std::optional and std::variant
    • Enforcing Code Contracts With [[nodiscard]]
    • Replacing enable_if with ifconstexpr
    • How to Parallelise CSV Reader
  • Appendix A - Compiler Support
  • Appendix B - Resources and Links

Part One, about the language features, is shorter and will give you a quick run over the most significant changes. You can read it in any order you like.

Part Two, describes a set of new types and utilities that were added to the Standard Library. The helper types create a potential new vocabulary for C++ code: like when you use optional, any, variant or string_view. And what’s more, you have new powerful capabilities, especially in the form of parallel algorithms and the standard filesystem. A lot of examples in this part will use many other features from C++17.

Part Three brings together all of the changes in the language and shows examples where a lot of new features are used alongside. You’ll see discussions about refactoring, simplifying code with new template techniques or working with parallel STL and the filesystem. While the first and the second part can also be used as a reference for individual changes, the third part shows more of larger C++17 patterns that join many features.

A considerable advantage of the book is the fact that with each new feature you’ll get information about the compiler support and the current implementation status. That way you’ll be able to check if a particular version of the most popular compilers (MSVC, GCC or Clang) implements it or not. The book also gives practical hints on how to apply new techniques in your current codebase.

Reader Feedback

If you spot an error, a typo, a grammar mistake… or anything else (especially logical issues!) that should be corrected, then please send your feedback to bartlomiej.filipek AT bfilipek.com.

You can also use those two places to leave your feedback:

Example Code

You can find the ZIP package with all the example on the book’s website:

cppindetail.com/data/cpp17indetail.zip

The same ZIP package should also be attached with the ebook.

Many examples in the book are relatively short. You can copy and paste the lines into your favourite compiler/IDE and then run the code snippet.

Code License

The code for the book is available under the Creative Commons License.

Compiling

To use C++17 make sure you provide a proper flag for your compiler:

  • for GCC (at least 7.1 or 8.0 or newer): use -std=c++17 or -std=c++2a
  • for Clang (at least 4.0 or newer): use -std=c++17 or -std=c++2a
  • for MSVC (Visual Studio 2017 or newer): use /std:c++17 or /std:c++latest in project options -> C/C++ -> Language -> C++ Language Standard

Formatting

The code is presented in a monospace font, similarly to the following example:

For longer examples with a corresponding cpp file:

ChapterABC/example_one.cpp
#include <iostream>

int main() {
    std::string text = "Hello World";
    std::cout << text << '\n';
}

Or shorter snippets (without a corresponding file):

int foo() {
    return std::clamp(100, 1000, 1001);
}

Snippets of longer programs were usually shortened to present only the core mechanics. In that case, you’ll find their full version in the separate ZIP package that comes with the book.

The corresponding file for the code snippet is mentioned in the title above the frame:

Chapter ABC/example_one.cpp

Usually, source code uses full type names with namespaces, like std::string, std::clamp, std::pmr. However, to make code compact and present it nicely on a book page the namespaces sometimes might be removed, so they don’t use space. Also, to avoid line wrapping, longer lines might be manually split into two. In some case, the code in the book might skip include statements.

Syntax Highlighting Limitations

The current version of the book might show some Pygments syntax highlighting limitations.

For example:

Other issues for C++ and Pygments: issues C++.

Online Compilers

Instead of creating local projects to play with the code samples, you can also leverage online compilers. They offer a basic text editor and usually allow you to compile only one source file (the code that you edit). They are convenient if you want to play with code samples and check the results using various compilers.

For example, many of the code samples for this book were created using Coliru Online and Wandbox compilers and then adapted for the book.

Here’s a list of some of the useful services:

  • Coliru - uses GCC 8.2.0 (as of July 2019), offers link sharing and a basic text editor, it’s simple but very effective.
  • Wandbox - offers a lot of compilers, including most Clang and GCC versions, can use boost libraries; offers link sharing and multiple file compilation.
  • Compiler Explorer - offers many compilers, shows compiler output, can execute the code.
  • CppBench - runs simple C++ performance tests (using google benchmark library).
  • C++ Insights - a Clang-based tool for source to source transformation. It shows how the compiler sees the code, for example by expanding lambdas, auto, structured bindings or range-based for loops.

There’s also a helpful list of online compilers gathered on this website: List of Online C++ Compilers.