Namespaces
Variants

C++ named requirements: BinaryPredicate

From cppreference.com
 
 
C++ named requirements
 

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:

  • pred is a FunctionObject
  • pred does not apply a non-constant function on its arguments
  • pred returns a BooleanTestable value
  • If pred(*it0, *it1) is called, then pred(elem0, elem1), pred(elem0, *it1) and pred(*it0, elem1) are all valid and equal to pred(*it0, *it1), regardless of whether elem0 and elem1 are const or not, regardless of whether elem0 and elem1 are lvalues or xvalues(since C++11), respectively
  • If pred(*it0, value) is called, then pred(elem0, value) is valid and equal to pred(*it0, value), regardless of whether elem0 is const or not, regardless of whether elem0 is 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)[edit]
searches for any one of a set of elements
(function template & algorithm function object)[edit]
finds the first two adjacent items that are equal (or satisfy a given predicate)
(function template & algorithm function object)[edit]
finds the first position where two ranges differ
(function template & algorithm function object)[edit]
determines if two sets of elements are the same
(function template & algorithm function object)[edit]
determines if a sequence is a permutation of another sequence
(function template & algorithm function object)[edit]
searches for the first occurrence of a range of elements
(function template & algorithm function object)[edit]
searches for the first occurrence of a number consecutive copies of an element in a range
(function template & algorithm function object)[edit]
removes consecutive duplicate elements in a range
(function template & algorithm function object)[edit]
creates a copy of some range of elements that contains no consecutive duplicates
(function template & algorithm function object)[edit]
(deprecated in C++17)(removed in C++20)
constructs custom std::binary_negate object
(function template) [edit]
removes consecutive duplicate elements
(public member function of std::forward_list<T,Allocator>) [edit]
removes consecutive duplicate elements
(public member function of std::list<T,Allocator>) [edit]
removes consecutive duplicate elements
(public member function of std::hive<T,Allocator>) [edit]
collection of unique keys, hashed by keys
(class template) [edit]
collection of key-value pairs, hashed by keys, keys are unique
(class template) [edit]
collection of keys, hashed by keys
(class template) [edit]
collection of key-value pairs, hashed by keys
(class template) [edit]

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 weak
to 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) [edit]