bind使用

来源:互联网 发布:香港网络的英文缩写 编辑:程序博客网 时间:2024/06/04 19:46
int add(int a, int b)
{
    return a + b;
}

int sub(int a, int b)
{
    return a - b;
}

template<typename T,typename V>
auto Test(const T&& t, const V& v1, const V& v2)->decltype(v1+v2)
{
    auto vv = bind(t, v1, v2);
    return vv();
}

int _tmain(int argc, _TCHAR* argv[])
{
    int v = Test(add, 10, 20);
    
    CTest t;
    t.show();

    system("pause");
    return 0;

}


#include <functional>

using namespace std::placeholders;

class CTest
{
public:
    CTest();
    ~CTest();

    int test(int a, int b)
    {
        return a + b;
    }

    void show()
    {
        callT = std::bind(&CTest::test, this, _1, _2);
        
        int va = callT(10, 20);
    }


private:
    using CallFun = std::function<int(int, int)>;
    CallFun callT;
};


0 0
原创粉丝点击