C++ named requirements: BinaryPredicate
Certain standard library functions take a BinaryPredicate parameter, called either with elements of two corresponding iterators or one iterator and a value whose type is part of the function signature.
Given BinaryPredicate pred, corresponding iterators it0 and it1, and value value, let elem0 designate the object *it0 and elem1 designate the object *it1, it is required that:
predis a FunctionObjectpreddoes not apply a non-constant function on its argumentspredreturns a BooleanTestable value- If
pred(*it0, *it1)is called, thenpred(elem0, elem1),pred(elem0, *it1)andpred(*it0, elem1)are all valid and equal topred(*it0, *it1), regardless of whetherelem0andelem1are const or not, regardless of whetherelem0andelem1are lvalues or xvalues(since C++11), respectively - If
pred(*it0, value)is called, thenpred(elem0, value)is valid and equal topred(*it0, value), regardless of whetherelem0is const or not, regardless of whetherelem0is an lvalue or xvalue(since C++11)
The most straight-forward way of achieving that last requirement is for pred to take its parameters by const lvalue reference. pred may take its parameters by value only if construction doesn't modify the source object; namely, if pred's first parameter and *it0 both have type T, or pred's second parameter and *it1 both have type T, then T must not have move semantics.
Standard library
The following standard library facilities expect a BinaryPredicate which isn't a Compare type.
| finds the last sequence of elements in a certain range (function template & algorithm function object) | |
(C++20) |
|
| searches for any one of a set of elements (function template & algorithm function object) | |
(C++20) |
|
| finds the first two adjacent items that are equal (or satisfy a given predicate) (function template & algorithm function object) | |
(C++20) |
|
| finds the first position where two ranges differ (function template & algorithm function object) | |
(C++20) |
|
| determines if two sets of elements are the same (function template & algorithm function object) | |
(C++20) |
|
(C++11) |
determines if a sequence is a permutation of another sequence (function template & algorithm function object) |
(C++20) |
|
| searches for the first occurrence of a range of elements (function template & algorithm function object) | |
(C++20) |
|
| searches for the first occurrence of a number consecutive copies of an element in a range (function template & algorithm function object) | |
(C++20) |
|
| removes consecutive duplicate elements in a range (function template & algorithm function object) | |
(C++20) |
|
| creates a copy of some range of elements that contains no consecutive duplicates (function template & algorithm function object) | |
(C++20) |
|
(deprecated in C++17)(removed in C++20) |
constructs custom std::binary_negate object (function template) |
| removes consecutive duplicate elements (public member function of std::forward_list<T,Allocator>)
| |
| removes consecutive duplicate elements (public member function of std::list<T,Allocator>)
| |
| removes consecutive duplicate elements (public member function of std::hive<T,Allocator>)
| |
(C++11) |
collection of unique keys, hashed by keys (class template) |
(C++11) |
collection of key-value pairs, hashed by keys, keys are unique (class template) |
(C++11) |
collection of keys, hashed by keys (class template) |
(C++11) |
collection of key-value pairs, hashed by keys (class template) |
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 2114 (P2167R3) |
C++98 | convertibility of return types to bool was too weakto reflect the expectation of implementations |
requirements strengthened |
| LWG 3031 | C++98 | requirements on const values were insufficient
|
requirements strengthened |
See also
| Predicate | |
(C++20) |
specifies that a callable type is a Boolean predicate (concept) |