2

I'm trying to understand how function binders work. So far I have got a small binder for 0-2 Arguments which works fine, but I don't know how to get it into one class (such as the function<>), to store it in a collection.

template <int>
struct placeholder {};

static const placeholder<1> _1_;
static const placeholder<2> _2_;

template <typename t>
struct list1
{
    list1(const t& i) : itm(i) {}
    t operator[](const placeholder<1>&) const {return itm;}
    template <typename it>
    it operator[](const it& _it) const {return _it;}

    template <typename fn>
    void operator()(fn func)
    {
        func(itm);
    }
    template <typename func, typename lst>
    void operator()(func fn, const lst& _lst)
    {
        fn(_lst[itm]);
    }
private:
    t itm;
};

template <typename t, typename t1>
struct list2
{
    list2(const t& i, const t1& i1) : itm(i), itm1(i1) {}
    t operator[](const placeholder<1>&) const {return itm;}
    t1 operator[](const placeholder<2>&) const {return itm1;}
    template <typename it>
    it operator[](const it& _it) const {return _it;}

    template <typename fn>
    void operator()(fn func)
    {
        func(itm, itm1);
    }
    template <typename func, typename lst>
    void operator()(func fn, const lst& _lst)
    {
        fn(_lst[itm], _lst[itm1]);
    }
private:
    t itm;
    t1 itm1;
};

template <typename func, typename lst>
class binder
{
public:
    binder(func _fn, const lst& _lst) : fn(_fn), _list(_lst) {}
    void operator()()
    {
        _list(fn);
    }
    template <typename a0>
    void operator()(const a0& _a0)
    {
        list1<a0> lst(_a0);
        _list(fn, lst);
    }
    template <typename a0, typename a1>
    void operator()(const a0& _a0, const a1& _a1)
    {
        list2<a0, a1> lst(_a0, _a1);
        _list(fn, lst);
    }
private:
    func fn;
    lst _list;
};

Normally I would use type erasure, but I just can't figure it out. I would need to inherit from a base class and make the operator()(...)-template virtual, which is not possible. Does anybody know how they solved it in the standard library?

Gonger96
  • 21
  • 2
  • I don't really understand what you want to achieve here, but im' sure i can help you – CollioTV Oct 03 '14 at 13:57
  • I would like to get the binder into a normal class. Just like the standard library does it (function<...> f = std::bind(...)) – Gonger96 Oct 03 '14 at 14:46

0 Answers0