C++ named requirements: Predicate
From cppreference.com
Certain standard library functions take a Predicate parameter, called with elements of a corresponding iterator.
Given Predicate pred and corresponding iterator it, let elem designate the object *it, it is required that:
predis a FunctionObjectpreddoes not apply a non-constant function on its argumentpredreturns a BooleanTestable valuepred(elem)is valid and equal topred(*it), regardless of whetherelemis const or not, regardless of whetherelemis an lvalue or xvalue(since C++11)
The most straight-forward way of achieving that last requirement is for pred to take its parameter by const lvalue reference. pred may take its parameter by value only if construction doesn't modify the source object; namely, if pred's parameter and *it both have type T, then T must not have move semantics.
Standard library
The following standard library functions take a Predicate parameter.
- std::all_of() (since C++11)
- std::any_of() (since C++11)
- std::none_of() (since C++11)
- std::copy_if()
- std::count_if()
- std::find_if()
- std::find_if_not()
- std::partition() (since C++11)
- std::remove_if()
- std::remove_copy_if()
- std::replace_if()
- std::stable_partition()
- std::is_partitioned() (since C++11)
- std::partition_copy() (since C++11)
- std::partition_point() (since C++11)
- std::forward_list::remove_if() (since C++11)
- std::list::remove_if()
Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
| DR | Applied to | Behavior as published | Correct behavior |
|---|---|---|---|
| LWG 3031 | C++98 | requirements on const values were insufficient
|
requirements strengthened |
See also
| BinaryPredicate | |
(C++20) |
specifies that a callable type is a Boolean predicate (concept) |