如何将函数绑定器放入一个类?

时间:2014-10-03 10:57:59

标签: c++ templates virtual-functions type-erasure function-binding

我试图了解功能绑定器的工作原理。到目前为止,我有一个小的活页夹0-2参数工作正常,但我不知道如何将它放入一个类(如函数<>),以将其存储在一个集合中。 / p>

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

通常我会使用类型擦除,但我无法弄明白。我需要从基类继承并使operator()(...) - 模板虚拟,这是不可能的。有人知道他们是如何在标准库中解决它的吗?

0 个答案:

没有答案
相关问题