mardi 4 avril 2017

different compare signature for std::upper_bound and std::lower_bound

Here is a sample example for both std::lower_bound & std::upper_bound, notice the signature of Compare lambda being passed to them -

const auto lower_x = std::lower_bound(
          points.begin(), points.end(), rec.min_corner.x,

          [](const RankedPoint &rp, const double x) { return rp.point.x < x; });

const auto upper_x = std::upper_bound(
          points.begin(), points.end(), rec.max_corner.x,

          [](const double x, const RankedPoint &rp) { return x < rp.point.x; });

What is the possible reasoning behind keeping the signature exact opposite of each other? I wasn't aware of this and gcc compiled (clang didn't) it when I used auto instead of definite types with wrong signatures. Costed me 10 minutes of frustration.

Aucun commentaire:

Enregistrer un commentaire