Widziałem coś, co wygląda na niespójność w std :: lower_bound() i std :: upper_bound() (dobrze, konwersja typu, naprawdę) i zastanawiałem się, czy ktoś mógłby to wyjaśnić? Zgodnie z komentarzem wiersz 2 nie będzie się kompilował pomimo oczywistego podobieństwa do wiersza 1; trzeba użyć postać pokazaną na linii 3 (na gcc 4.7.3/ubuntu 64-bitowych przynajmniej - to wszystko co mam do zabawy)Niespójne wymagania dotyczące wartości nadrzędnych i dolnych:
#include <set>
#include <algorithm>
using namespace std;
class MyInt {
private:
int val;
public:
MyInt(int _val): val(_val) {}
bool operator<(const MyInt& other) const {return val < other.val;}
};
int main() {
set<MyInt> s;
s.insert(1); // demonstrate implicit conversion works
s.insert(MyInt(2));
s.insert(3); // one last one for the road
set<MyInt>::iterator itL = lower_bound(s.begin(), s.end(), 2); //LINE 1
// the line below will NOT compile
set<MyInt>::iterator itU = upper_bound(s.begin(), s.end(), 2); //LINE 2
// the line below WILL compile
set<MyInt>::iterator itU2 = upper_bound(s.begin(), s.end(), MyInt(2)); // LINE 3
return 0;
}
To samo zachowanie z g ++ 4.8.4 tutaj. Z pewnością jest to błąd g ++. –