简体   繁体   English

std :: upper_bound()的库引用和编译器之间存在奇怪的差异

[英]Strange discrepancy between library reference and compiler for std::upper_bound()

I need to process a list of objects of type Foo in groups sharing the quality of corresponding to the same value of Bar . 我需要在组中处理Foo类型的对象列表,这些组共享对应于相同Bar值的质量。 The list is pre-sorted in relation to that quality, so my idea was to use std::upper_bound to find where the subsequent groups begin. 该列表是根据该质量预先排序的,因此我的想法是使用std::upper_bound来查找后续组的开始位置。

Bar FooToBar(const Foo &foo);
// sorted so that FooToBar(foolist[0] <= FooToBar(foolist[1]) <= ...
std::list<Foo> foolist; 

// find bounds of a group of Foo-s corresponding to someBar;
Bar someBar;
auto 
    groupBegin = foolist.begin(),
    // find last item of foolist whose FooToBar() == someBar
    groupEnd   = std::upper_bound( foolist.begin(), 
                                   foolist.end(), 
                                   someBar ); 

Of course that will not work because Foo and Bar are not directly comparable. 当然这不起作用,因为FooBar不能直接比较。 Luckily, there's an overload of std::upper_bound which takes an extra comparator argument: 幸运的是, std::upper_bound有一个重载,需要一个额外的比较器参数:

groupEnd = std::upper_bound( foolist.begin(), foolist.end(), someBar, Compare);

Question is, how do I go about writing Compare() ? 问题是,如何编写Compare() Here's where things get interesting. 事情变得有趣。 cppreference.com says: cppreference.com说:

The signature of the comparison function should be equivalent to the following: 比较函数的签名应等效于以下内容:

bool cmp(const Type1 &a, const Type2 &b); bool cmp(const Type1&a,const Type2&b);

The signature does not need to have const &, but the function object must not modify the objects passed to it. 签名不需要具有const&,但是函数对象不能修改传递给它的对象。 The types Type1 and Type2 must be such that an object of type T can be implicitly converted to both Type1 and Type2, and an object of type ForwardIt can be dereferenced and then implicitly converted to both Type1 and Type2. 类型Type1和Type2必须使得类型为T的对象可以隐式转换为Type1和Type2,并且可以取消引用类型为ForwardIt的对象,然后将其隐式转换为Type1和Type2。

Obviously, there's no way I can satisfy those conditions with Foo and Bar . 显然,我无法用FooBar满足这些条件。 However, cplusplus.com says something different: 然而, cplusplus.com说了一些不同的东西:

Binary function that accepts two arguments (the first is always val, and the second of the type pointed by ForwardIterator), and returns a value convertible to bool. 接受两个参数的二进制函数(第一个是val,第二个是ForwardIterator指向的类型),并返回一个可转换为bool的值。

I can work with that, so: 我可以使用它,所以:

bool Compare(const Bar &bar, const Foo &foo) { /* ... */ }

However, that does not compile in either VS2013, nor g++: 但是,这不能在VS2013和g ++中编译:

/usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/bits/predefined_ops.h:141:37: error: cannot convert 'Foo' to 'Bar' in argument passing /usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/bits/predefined_ops.h:141:37:错误:在参数传递中无法将'Foo'转换为'Bar'

Curiously, when I reverse the argument order, it compiles, runs and behaves as expected: 奇怪的是,当我反转参数顺序时,它会按预期编译,运行和运行:

bool Compare(const Foo &foo, const Bar &bar) { /* ... */ }

So it looks like one reference says one thing, other reference says something else, and the compiler accepts something still different. 所以它看起来像一个引用说了一件事,其他引用说了别的东西,编译器接受了一些不同的东西。 Or did I misunderstand something? 还是我误解了什么?

What you're refering to is a defect in the standard: #270 . 你所指的是标准中的缺陷: #270 The original wording was deemed to strict (indeed, your particular use case was mentioned). 原来的措辞被认为是严格的(实际上,你提到了你的特定用例)。 The section in the Standard now reads, [upper.bound]: 标准中的部分现在读取,[upper.bound]:

 template<class ForwardIterator, class T> ForwardIterator upper_bound(ForwardIterator first, ForwardIterator last, const T& value); template<class ForwardIterator, class T, class Compare> ForwardIterator upper_bound(ForwardIterator first, ForwardIterator last, const T& value, Compare comp); 

Requires : The elements e of [first,last) shall be partitioned with respect to the expression !(value < e) or !comp(value, e) . 要求[first,last)的元素e应根据表达式!(value < e)!comp(value, e)进行分区。
Returns : The furthermost iterator i in the range [first,last] such that for every iterator j in the range [first,i) the following corresponding conditions hold: !(value < *j) or comp(value, *j) == false . 返回 :最远迭代i在范围[first,last] ,从而使每一迭代j范围[first,i)下列相应条件成立: !(value < *j)comp(value, *j) == false

In both cases, value is the first argument to comp and the element is second. 在这两种情况下, valuecomp的第一个参数,元素是second。 So the following is perfectly valid code: 所以以下是完全有效的代码:

struct Foo { };
struct Bar { };

std::vector<Foo> foolist;

auto it = std::upper_bound(foolist.begin(), foolist.end(), Bar{}, 
                           [](Bar const&, Foo const&) { return false; });

The above works on both gcc 5.2 (and even 4.6.4 -- modulo the lambda -- which is the oldest I have easy access to) and clang 3.6. 上面的工作在gcc 5.2(甚至4.6.4 - modulo lambda - 这是我能够轻松访问的最老的)和clang 3.6。

If you read the part of the documentation that you quoted in your question, you will understand that unless there is an implicit conversion from Bar to Foo , both versions of Compare are incorrect. 如果您阅读了在问题中引用的文档部分,您将理解除非存在从BarFoo的隐式转换,否则两个版本的Compare都不正确。 The fact that one version works is merely a lucky coincidence. 一个版本有效的事实仅仅是一个幸运的巧合。 It may easily fail with a different compiler. 使用不同的编译器可能很容易失败。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM