[algs] Add hb_bind0 and hb_bind1

This commit is contained in:
Behdad Esfahbod 2019-05-15 20:48:20 -07:00
parent d214b07883
commit 16a3540ea4
2 changed files with 61 additions and 0 deletions

View File

@ -119,6 +119,60 @@ struct
}
HB_FUNCOBJ (hb_invoke);
enum class hb_bind_pos_t
{
_0,
_1,
};
template <typename Appl, hb_bind_pos_t P, typename V>
struct hb_binder_t
{
hb_binder_t (Appl a, V v) : a (a), v (v) {}
template <typename ...Ts,
hb_bind_pos_t PP = P,
hb_enable_if (PP == hb_bind_pos_t::_0)> auto
operator () (Ts&& ...ds) -> decltype (hb_invoke (hb_declval (Appl),
hb_declval (V),
hb_declval (Ts)...))
{
return hb_invoke (hb_forward<Appl> (a),
hb_forward<V> (v),
hb_forward<Ts> (ds)...);
}
template <typename T0, typename ...Ts,
hb_bind_pos_t PP = P,
hb_enable_if (PP == hb_bind_pos_t::_1)> auto
operator () (T0&& d0, Ts&& ...ds) -> decltype (hb_invoke (hb_declval (Appl),
hb_declval (T0),
hb_declval (V),
hb_declval (Ts)...))
{
return hb_invoke (hb_forward<Appl> (a),
hb_forward<T0> (d0),
hb_forward<V> (v),
hb_forward<Ts> (ds)...);
}
private:
hb_reference_wrapper<Appl> a;
V v;
};
struct
{
template <typename Appl, typename V> auto
operator () (Appl&& a, V&& v) const HB_AUTO_RETURN
(( hb_binder_t<Appl, hb_bind_pos_t::_0, V> (a, v) ))
}
HB_FUNCOBJ (hb_bind0);
struct
{
template <typename Appl, typename V> auto
operator () (Appl&& a, V&& v) const HB_AUTO_RETURN
(( hb_binder_t<Appl, hb_bind_pos_t::_1, V> (a, v) ))
}
HB_FUNCOBJ (hb_bind1);
struct
{
private:

View File

@ -77,5 +77,12 @@ main (int argc, char **argv)
xp = hb_pair_t<int *, double> (nullptr, 1);
xp = hb_pair_t<const int*, int> (nullptr, 1);
assert (3 == hb_bind0 (hb_min, 3) (4));
assert (3 == hb_bind0 (hb_min, 4) (3));
auto M0 = hb_bind1 (hb_max, 0);
assert (M0 (-2) == 0);
assert (M0 (+2) == 2);
return 0;
}