Nicolai M. Josuttis: solutions in time  C++ Templates, 2nd Ed.: Errata for all Printings

C++ Templates - The Complete Guide, Second Edition

Errata for all Printings
Sep 18, 2017

This is the errata of the 1st printing of the book C++ Templates, 2nd Edition by David Vandevoorde, Nicolai M. Josuttis, and Douglas Gregor.
It covers all errors that were found.

The errata is organized in the following way:
- The first part lists technical errors
- The second part lists typos


Errors

In general, throughout the book:

We forgot to make more clear that before C++17 you have to add a definition to a static constexpr declaration of a class member.

struct A {
  static constexpr int n = 5; // C++11/C++14: declaratio, since C++17: definition
};

Before C++17 this a declaration of n only. Only since C++17 this is also a definition.
So, before C++17 in exactly one translation unit you have to provide a corresponding definion, which we also missed in several examples:

// in one translation unit before C++17:
constexpr int A::n; // C++11/C++14: definition (since C++17: deprecated redundant declaration)

The reason, nobody saw this before the book was out is that often the definition is not necessary at all. In fact, you can alwas pass the member by value. Only when passing these members by reference and if the compiler doesn't optimize the call away link errors occur:

std::cout << A::n;       // OK even without definition (ostream::operator<<(int) gets A::n by value)

int inc(const int& i);   // pass by reference!
std::cout << inc(A::n);  // link error without definition and strong optimizations


Typos

Page 7, Footnote: s/Visual Studio 20133/Visual Studio 2013/

Page 40, 1st para of 2.9: s/(that don’t have a default value),/(that don’t have a default value)./

Page 40, 1st code snippet of 2.9: s/Stack<int> intStack1; // stack of strings/Stack<int> intStack1; // stack of ints/

Page 41, 1st bullet: s/Due to the definition of the int constructor, /Due to the definition of the constructor template, /

 


Home of the C++ Templates book