C++泛型编程学习 std::function

void test(int var)
{
    cout << var < fun)
{
    fun(120)
};
eg:
    calltest(test);
    
    calltest([](int var)->void{
        cout<< "lambda ===" << var << endl;
})




/*=============================================*/
template
class CFObjHandler
{
public:
virtual T invoke(Args...args)const{};

}
template
class CFObjHandlerChild : public CFObjHandler
{
public:
CFObjHandlerChild(U&& var):functor(std::forward(var))
{
}
virtual T invoke(Args...args)const
{
    return functor(std::forward(args)...)
};
private:
    U functor;
}


template
class CallFuncObj;

template
class CallFuncObj
{
    template
    CallFuncObj(U&& acobj)
    {
        hander = new CFObjHandlerChild(std::forward(acobj));
    }
    ~CallFuncObj()
    {
        delete handler;
    }
public:
    T operator()(Args... args) const
    {
        return hander->invoke(std::forward(args)...);
    }
private:
    CFObjHandler*hander;
};

void callObjFun(CallFuncObj cobj)
{
    cobj(120);
}

 

你可能感兴趣的:(C/C++)