Chapter 4.4

Con.5: Use constexpr for values that can be computed at compile time

From const to constexpr

Prior to C++11, const machinery was restricted to two things: qualifying a type as const, and thus any instance of that type as immutable, and qualifying a nonstatic member function such that *this is const in its body, like this:

class int_wrapper {
public:
  explicit int_wrapper(int i);
  void mutate(int i);
  int inspect() const;

private:
  int m_i;
};

auto const i = int_wrapper{7}; // i is of type int_wrapper const
// i.mutate(5); // Cannot invoke non-const qualified
                // member function on const object
auto j = i.inspect(); // Assign from inspect

We are sure you are familiar with this. You may also be familiar with ...

Get Beautiful C++: 30 Core Guidelines for Writing Clean, Safe, and Fast Code now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.