liveBook · Manning

Predicate

As explained in 100 C++ Mistakes and How to Avoid Them

A predicate is a Boolean-valued function that determines whether a value satisfies a condition.

A predicate is a function that returns true or false. In filtering, it decides whether each value remains in the output or is rejected. section 4.4

Mistake: hiding a Boolean result behind unnecessary control flow

This verbose predicate initializes a variable, assigns true or false through an if/else, and returns it. Since the comparison already computes the answer, return it directly: `bool isEven(int n) { return n % 2 == 0; }` The shorter version is easier to read and maintain. section 12.4

Predicates also compose with ranges. A transform can normalize values before a filter predicate retains only values satisfying its condition, such as evenness. section 4.4

How 100 C++ Mistakes and How to Avoid Them covers it

How deep it goes
Defines predicates, demonstrates filtering, and refactors a verbose Boolean implementation without descending into internals.
Examples
Uses even-number testing, absolute-value transformation, range filtering, and accumulation over retained values.
What it assumes
Assumes basic functions, conditionals, modulus, containers, and value sequences while reinforcing Boolean return behavior.
What you can do after
Apply it via std::views::filter and write concise functions that return computed Boolean results directly.
How it connects
Connects predicates with filtering, ranges, containers, mapping, reduction, and Boolean expressions.

Also explained in

Compare how 5 books explain predicate →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of 100 C++ Mistakes and How to Avoid Them. The section links point to the parts of the book it draws on.