std::search
的二元谓词的约束之一是:
Type1和Type2的类型必须是这样的,即ForwardIt1和ForwardIt2类型的对象可以被取消引用,然后隐式转换为Type1和Type2。
然而,在C++国际标准文档中,我没有看到这样的约束。我想知道这个约束是否在C++国际标准文档中明确说明,并且它属于哪个部分。此外,如果不是这样,为什么要将此约束放置在std::search
函数上,因为有可能存在两种不同的类型,虽然不能隐式转换,但定义了相等运算符重载?
std::search
的二元谓词的约束之一是:
Type1和Type2的类型必须是这样的,即ForwardIt1和ForwardIt2类型的对象可以被取消引用,然后隐式转换为Type1和Type2。
然而,在C++国际标准文档中,我没有看到这样的约束。我想知道这个约束是否在C++国际标准文档中明确说明,并且它属于哪个部分。此外,如果不是这样,为什么要将此约束放置在std::search
函数上,因为有可能存在两种不同的类型,虽然不能隐式转换,但定义了相等运算符重载?
pred(*first1, *first2)
调用的二元谓词和既作为pred(*first1, *first2)
又作为pred(*first2, *first1)
调用的二元谓词。cast req2
使用添加了一个命名参数,它控制着它是说"both"还是"respectively"。 eq
变体没有这个新参数的传递。我没有编辑权限将其添加到模板和std::search
的条目中。bidirectional
(MediaWiki 的 #if
是“如果为空”检查,而不是“如果为真”检查)。我已经对模板进行了检测,以便找出我们在使用此默认值的位置,以便可以修复它们(通过更改默认值或使用 bidirectional=false
进行抑制)。 - T.C.BinaryPredicate
的要求在[algorithms.requirements]/7中说明:
每当一个算法期望一个应用于解引用两个相应迭代器的结果或者解引用一个迭代器和类型
T
(当T
是函数签名的一部分时)时返回一个可测试为true
值的函数对象时,就会使用BinaryPredicate
参数。换句话说,如果一个算法将BinaryPredicate binary_pred
作为其参数,并将first1
和first2
作为其迭代器参数,它应该在上下文转换为bool
([conv])的构造binary_pred(*first1, *first2)
中正确地工作。BinaryPredicate
总是将第一个迭代器的value_type
作为其第一个参数;也就是说,在这些情况下,当T value
是函数签名的一部分时,它应该在上下文转换为bool
([conv])的构造binary_pred(*first1, value)
中正确地工作。binary_pred
不应通过解引用的迭代器应用任何非常量函数。
没有要求从解引用ForwardIt2
构造Type1
。cppreference中OP引用的“both”一词是不准确的。
first1
和 first2
这两种类型必须隐式转换为同一类型,这是网站声明的一个限制条件。我猜这是否意味着我提供的链接有错误的解释? - cogle好的,我终于看到你的困惑了。这是由于“both”一词的用法引起的。
意思应该是以下两个条件都必须为真:
类型Type1
必须使得ForwardIt1
类型的对象可以被取消引用,然后隐式转换为Type1
类型Type2
必须使得ForwardIt2
类型的对象可以被取消引用,然后隐式转换为Type2
相关的是条件:§25.2.13 Search [alg.search]
template<class ForwardIterator1, class ForwardIterator2, class BinaryPredicate> ForwardIterator1 search(ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred);
- Returns: The first iterator
i
in the range[first1,last1 - (last2-first2))
such that for every non-negative integern
less thanlast2 - first2
the following corresponding conditions hold:*(i + n) == *(first2 + n)
,pred(*(i + n), *(first2 + n)) != false
. Returnsfirst1
if[first2, last2)
is empty, otherwise returnslast1
if no such iterator is found.
pred(*(i + n), *(first2 + n)) != false
这意味着pred(*(i + n), *(first2 + n))
是有效的。也就是说,*(i + n)
和*(first2 + n)
可以作为参数传递给pred。意味着pred的第一个参数必须可以从对ForwardIterator1进行解引用构造,而pred的第二个参数必须可以从对ForwardIterator2进行解引用构造。