Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ binary predicate implementation requirements for std::search

Tags:

c++

c++11

On this site it states that one of the constraints for a binary predicate passed into std::search is that:

The types Type1 and Type2 must be such that objects of types ForwardIt1 and ForwardIt2 can be dereferenced and then implicitly converted to both Type1 and Type2.

However, in the C++ International Standard documentation I see no reference to such a constraint. I am wondering if this constraint is explicitly stated in the C++ International Standard documentation which section it is under. In addition if it is not then why is this constraint placed upon the std::search function as isn't it possible to have two distinct types that while not implicitly convertible have an equality operator overload defined?

like image 479
cogle Avatar asked Mar 02 '18 10:03

cogle


2 Answers

It's a mistake on the site.

That parameter description is populated from this template. It doesn't distinguish between binary predicates that are only called as pred(*first1, *first2) from those that are called as both pred(*first1, *first2) and pred(*first2, *first1)

Further digging: It appears that this edit added a named parameter to the parent template's use of cast req2, which controls whether it says "both" or "respectively". The eq variant does not have a passthrough of this new parameter. I don't have edit privileges to add it in to the template and the entry for std::search

like image 162
Caleth Avatar answered Sep 29 '22 00:09

Caleth


The requirement of a BinaryPredicate is stated in [algorithms.requirements]/7:

The BinaryPredicate parameter is used whenever an algorithm expects a function object that when applied to the result of dereferencing two corresponding iterators or to dereferencing an iterator and type T when T is part of the signature returns a value testable as true. In other words, if an algorithm takes BinaryPredicate binary_­pred as its argument and first1 and first2 as its iterator arguments, it should work correctly in the construct binary_­pred(*first1, *first2) contextually converted to bool ([conv]). BinaryPredicate always takes the first iterator's value_­type as its first argument, that is, in those cases when T value is part of the signature, it should work correctly in the construct binary_­pred(*first1, value) contextually converted to bool ([conv]). binary_­pred shall not apply any non-constant function through the dereferenced iterators.

There is no requirement for constructing Type1 from dereferencing ForwardIt2. The word 'both' in OP's quotation from cppreference is inaccurate.

like image 43
llllllllll Avatar answered Sep 29 '22 00:09

llllllllll