Chapter 19

Profiles

First of all: What is a profile according to the C++ Core Guidelines? Here is their definition: “A ‘profile’ is a set of deterministic and portably enforceable subset rules (i.e., restrictions) that are designed to achieve a specific guarantee.”

Two terms in this definition are particularly interesting:

There are two main reasons for the profiles:

  1. You have to deal with legacy code, and you cannot apply all rules of the C++ Core Guidelines in one step. You have to apply the rules step by step and, therefore, use some rules first and some rules later.

  2. Some related rules may be more important to your code base than others. They aim for a specific goal such as the “avoidance of bounds errors” or the “correct usage of types.” These related rules are called profiles.

The C++ Core Guidelines provide profiles for type safety, bounds safety, and lifetime safety, which can be automatically checked. Read more details about automatic checks in Appendix A, Enforcing the C++ Core Guidelines.

The following sections give a concise overview of the three profiles.

Pro.typeType safety

Pro.boundsBounds safety

  • Bounds safety: Operate inside the bounds of allocated memory.

    The two enemies for bounds safety are pointer arithmetic and array indexing. Additionally, when you use a pointer, it should only address a single object but not an array. To make the profile bounds safety complete, you should combine it with the rules to type safety and lifetime safety.

Bounds safety consists of four rules:

Pro.lifetimeLifetime safety

  • Lifetime safety: Dereference only a valid pointer.

    A pointer is invalid if, for example, the pointer is uninitialized, is a std::nullptr, points outside the range of an array, or points to a deleted object. The profile lifetime safety consists of one rule:

  • Lifetime.1: Don’t dereference a possibly invalid pointer: ES.65: Don’t dereference an invalid pointer