Difference between revisions of "cpp/named req/Predicate"
m (Added Spanish link) |
(LWG 3031) |
||
Line 14: | Line 14: | ||
In other words, if an algorithm takes a {{named req/core|Predicate}} {{tt|pred}} and an iterator {{tt|first}}, it should be able to test the object of the type pointed to by the iterator {{tt|first}} using the given predicate via a construct like {{c|if(pred(*first)) {...} }}. | In other words, if an algorithm takes a {{named req/core|Predicate}} {{tt|pred}} and an iterator {{tt|first}}, it should be able to test the object of the type pointed to by the iterator {{tt|first}} using the given predicate via a construct like {{c|if(pred(*first)) {...} }}. | ||
− | The function object {{tt|pred}} shall not apply any non-constant function through the dereferenced iterator. This function object may be a pointer to function or an object of a type with an appropriate function call operator. | + | The function object {{tt|pred}} shall not apply any non-constant function through the dereferenced iterator {{rev inl|since=c++20<!--LWG 3031-->| and must accept a {{tt|const}} object argument, with the same behavior regardless of whether its argument is {{tt|const}} or non-{{tt|const}}}}. This function object may be a pointer to function or an object of a type with an appropriate function call operator. |
===Requirements=== | ===Requirements=== |
Revision as of 02:49, 4 May 2022
The Predicate requirements describe a callable that returns a value testable as a bool.
Predicate is typically used with algorithms that take input data (individual objects/containers) and a predicate, which is then called on input data to decide on further course of action. Some examples of predicate usage in C++ standard library are:
- std::all_of, std::any_of, std::none_of Take an array of elements and a predicate as an input. Call predicate on individual input elements, and return true if for all/any/none elements, predicate returns true.
- std::find_if Take sequence of elements, and a predicate. Return first element in the sequence, for which predicate returns value equal to true
Description of algorithm facilities, given above, is crude and intended to explain Predicate in simple terms. For detailed info, refer to individual pages.
In other words, if an algorithm takes a Predicate pred
and an iterator first
, it should be able to test the object of the type pointed to by the iterator first
using the given predicate via a construct like if(pred(*first)) {...}.
The function object pred
shall not apply any non-constant function through the dereferenced iterator and must accept a const
object argument, with the same behavior regardless of whether its argument is const
or non-const
(since C++20). This function object may be a pointer to function or an object of a type with an appropriate function call operator.
Requirements
This section is incomplete Reason: better describe actual requirements |