From 6fa1f38070e710b2f80a836bd633b6ab33e1bc80 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Tue, 7 May 2019 21:33:26 -0700 Subject: [PATCH] [algs] Accept varargs in hb_min/max --- src/hb-algs.hh | 24 +++++++++++++++++++++--- src/test-algs.cc | 8 ++++++++ 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/src/hb-algs.hh b/src/hb-algs.hh index 7ed42d6..3b5e45f 100644 --- a/src/hb-algs.hh +++ b/src/hb-algs.hh @@ -186,20 +186,38 @@ struct } HB_FUNCOBJ (hb_second); -/* Note. In min/max, we can use hb_type_identity for second argument. +/* Note. In min/max impl, we can use hb_type_identity for second argument. * However, that would silently convert between different-signedness integers. * Instead we accept two different types, such that compiler can err if * comparing integers of different signedness. */ struct { + private: template auto - operator () (T&& a, T2&& b) const HB_AUTO_RETURN (a <= b ? a : b) + impl (T&& a, T2&& b) const HB_AUTO_RETURN (a <= b ? a : b) + + public: + template auto + operator () (T&& a) const HB_AUTO_RETURN (a) + + template auto + operator () (T&& a, Ts&& ...ds) const HB_AUTO_RETURN + (impl (hb_forward (a), (*this) (hb_forward (ds)...))) } HB_FUNCOBJ (hb_min); struct { + private: template auto - operator () (T&& a, T2&& b) const HB_AUTO_RETURN (a >= b ? a : b) + impl (T&& a, T2&& b) const HB_AUTO_RETURN (a >= b ? a : b) + + public: + template auto + operator () (T&& a) const HB_AUTO_RETURN (a) + + template auto + operator () (T&& a, Ts&& ...ds) const HB_AUTO_RETURN + (impl (hb_forward (a), (*this) (hb_forward (ds)...))) } HB_FUNCOBJ (hb_max); diff --git a/src/test-algs.cc b/src/test-algs.cc index 163a79a..0a8fcbf 100644 --- a/src/test-algs.cc +++ b/src/test-algs.cc @@ -62,5 +62,13 @@ main (int argc, char **argv) A a; hb_invoke (&A::a, a); + assert (1 == hb_min (3, 8, 1, 2)); + assert (8 == hb_max (3, 8, 1, 2)); + + int x = 1, y = 2; + int &z = hb_min (x, y); + z = 3; + assert (x == 3); + return 0; } -- 2.7.4