The C++ standard library implements numerous common algorithms that accept a predicate function object. The C++ Standard, [algorithms.general], paragraph 10 [ISO/IEC 14882-2014], states the following:
[Note: Unless otherwise specified, algorithms that take function objects as arguments are permitted to copy those function objects freely. Programmers for whom object identity is important should consider using a wrapper class that points to a noncopied implementation object such as
reference_wrapper<T>, or some equivalent solution. — end note]
Because it is implementation-defined whether an algorithm copies a predicate function object, any such object must either
- implement a function call operator that does not mutate state related to the function object's identity, such as nonstatic data members or captured values, or
- wrap the predicate function object in a
std::reference_wrapper<T>(or an equivalent solution).
Marking the function call operator as
const is beneficial, but insufficient, because data members with the
mutable storage class specifier may still be modified within a
const member function.
Noncompliant Code Example (Functor)
This noncompliant code example attempts to remove the third item in a container using a predicate that returns
true only on its third invocation.
std::remove_if() is permitted to construct and use extra copies of its predicate function. Any such extra copies may result in unexpected output.
This result arises because
std::remove_if makes two copies of the predicate before invoking it. The first copy is used to determine the location of the first element in the sequence for which the predicate returns
true. The subsequent copy is used for removing other elements in the sequence. This results in the third element (
2) and sixth element (
5) being removed; two distinct predicate functions are used.
Noncompliant Code Example (Lambda)
Similar to the functor noncompliant code example, this noncompliant code example attempts to remove the third item in a container using a predicate lambda function that returns
true only on its third invocation. As with the functor, this lambda carries local state information, which it attempts to mutate within its function call operator.
Compliant Solution (
This compliant solution wraps the predicate in a
std::reference_wrapper<T> object, ensuring that copies of the wrapper object all refer to the same underlying predicate object.
The above compliant solution demonstrates using a reference wrapper over a functor object but can similarly be used with a stateful lambda. The code produces the expected results, where only the third element is removed.
Compliant Solution (Iterator Arithmetic)
Removing a specific element of a container does not require a predicate function but can instead simply use
std::vector::erase(), as in this compliant solution.
Using a predicate function object that contains state can produce unexpected values.
Make predicates const pure functions
3225, 3226, 3227, 3228, 3229,
3230, 3231, 3232, 3233, 3234