[algs] Accept varargs in hb_min/max

This commit is contained in:
Behdad Esfahbod 2019-05-07 21:33:26 -07:00
parent 1ad07080c3
commit 6fa1f38070
2 changed files with 29 additions and 3 deletions

View File

@ -186,20 +186,38 @@ struct
}
HB_FUNCOBJ (hb_second);
/* Note. In min/max, we can use hb_type_identity<T> for second argument.
/* Note. In min/max impl, we can use hb_type_identity<T> 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 <typename T, typename T2> 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 <typename T> auto
operator () (T&& a) const HB_AUTO_RETURN (a)
template <typename T, typename... Ts> auto
operator () (T&& a, Ts&& ...ds) const HB_AUTO_RETURN
(impl (hb_forward<T> (a), (*this) (hb_forward<Ts> (ds)...)))
}
HB_FUNCOBJ (hb_min);
struct
{
private:
template <typename T, typename T2> 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 <typename T> auto
operator () (T&& a) const HB_AUTO_RETURN (a)
template <typename T, typename... Ts> auto
operator () (T&& a, Ts&& ...ds) const HB_AUTO_RETURN
(impl (hb_forward<T> (a), (*this) (hb_forward<Ts> (ds)...)))
}
HB_FUNCOBJ (hb_max);

View File

@ -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;
}