functional.hpp 1.39 KB
Newer Older
Edwin Carlinet's avatar
Edwin Carlinet committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#ifndef FUNCTIONAL_HPP
# define FUNCTIONAL_HPP

# include <functional>

namespace mln
{

  //
  // contrary to std::bind, it can be DefaultConstructible and Assignable
  template <typename BinaryFunction, typename V>
  struct binder1st
  {
  private:
    BinaryFunction fun_;
    V v_;

  public:
    binder1st() = default;
    binder1st(const BinaryFunction& f, const V& v)
      : fun_(f), v_ (v)
    {
    }

    template <typename T>
    typename std::result_of< BinaryFunction(V, T) >::type
    operator () (T&& x) const
    {
      return fun_(v_, std::forward<T>(x));
    }
  };

  template <typename BinaryFunction, typename V>
  struct binder2nd
  {
  private:
    BinaryFunction fun_;
    V v_;

  public:
    binder2nd() = default;

    binder2nd(const BinaryFunction& f, const V& v)
      : fun_(f), v_ (v)
    {
    }

    template <typename T>
    typename std::result_of< BinaryFunction(T, V) >::type
    operator () (T&& x) const
    {
      return fun_(std::forward<T>(x), v_);
    }
  };

  template <typename BinaryFunction, typename V>
  inline
  binder1st<BinaryFunction, V>
  bind1st(const BinaryFunction& f, const V& v)
  {
    return binder1st<BinaryFunction, V>(f, v);
  }

  template <typename BinaryFunction, typename V>
  inline
  binder2nd<BinaryFunction, V>
  bind2nd(const BinaryFunction& f, const V& v)
  {
    return binder2nd<BinaryFunction, V>(f, v);
  }

}

#endif // ! FUNCTIONAL_HPP