std::bind

参考资料


• cplusplus.comhttp://www.cplusplus.com/reference/functional/bind/

• cppreference.comhttp://en.cppreference.com/w/cpp/utility/functional/bind

std::bind简介


• 函数模板声明

// cplusplus.com
// simple(1)
template <class Fn, class... Args>
/* unspecified */ bind (Fn&& fn, Args&&... args);

// with return type (2)
template <class Ret, class Fn, class... Args>
/* unspecified */ bind (Fn&& fn, Args&&... args);

// GCC 4.8.2 - /usr/include/c++/4.8.2/tr1
template <typename _Functor, typename... _ArgTypes>
inline _Bind<typename _Maybe_wrap_member_pointer<_Functor>::type(_ArgTypes...)>
bind(_Functor __f, _ArgTypes... __args)
{
...
}

template <typename _Result, typename _Functor, typename... _ArgTypes>
inline _Bind_result<_Result, typename _Maybe_wrap_member_pointer<_Functor>::type (_ArgTypes...)>
bind(_Functor __f, _ArgTypes... __args)
{
...
}

// MS C++ 2013 Microsoft Visual Studio 12.0VCincludefunctional
template <class _Funclass... _Types>
inline _Bind<false, void, _Fun, _Types...>
bind(_Fun && _Fx, _Types &&... _Args)
// bind a function object
...
}

template <class _Rxclass... _Ftypesclass... _Types>
inline _Bind<true, _Rx, _Rx (* const)(_Ftypes...), _Types...>
bind(_Rx (*_Pfx)(_Ftypes...), _Types&&... _Args)
// bind a function pointer
...
}

template <class _Rxclass _Farg0class... _Types>
inline _Bind<false, void, _Pmd_wrap<_Rx _Farg0::*, _Rx, _Farg0>, _Types...>
bind(_Rx _Farg0::* const _Pmd, _Types&&... _Args)
// bind a wrapped member object pointer
...
}

#define _IMPLICIT_PMF_WRAP(CALL_OPT, X1, CV_OPT)
template <class _Rx, class _Farg0, class... _Ftypes, class... _Types>
inline _Bind<true, _Rx, _Pmf_wrap<_Rx(CALL_OPT _Farg0::*)(_Ftypes...) CV_OPT, _Rx, _Farg0, _Ftypes...>, _Types...>
bind(_Rx(CALL_OPT _Farg0::* const _Pmf)(_Ftypes...) CV_OPT, _Types&&... _Args)
{ /* bind a wrapped CV_OPT member function pointer */
...
}

_MEMBER_CALL_CV(_IMPLICIT_PMF_WRAP, )
...

• 函数模板说明

       以cplusplus.com中描述的原型说明:

       基于Fn参数返回一个函数对象,并且以Args参数绑定为函数对象的参数。每个参数要么绑定一个参数值,要么绑定为一个std::placeholders。如果参数绑定成一个值,那么返回的函数对象将总使用绑定的参数值做为调用参数,即调用传入参数将不起作用;如果参数绑定为std::placeholders,那么返回的函数对象在被调用时需要传入实时参数,参数填充的位置即由placeholder指定的序号。

       bind函数返回的函数对象类型和Fn一致,除非用户在Ret参数中指定了返回类型。需要注意的是,Ret参数只是一个模板参数,它并不能由传入该函数的参数进行隐式推导。

• 模板参数说明

       以cplusplus.com中描述的原型说明:

       Fn    :  函数对象、普通函数指针或类成员函数指针。

       Args : 用于绑定的参数列表。其中每个参数要么是参数值要么是一个placeholder

std::bind详解


• 绑定普通函数

#include <iostream>
#include <functional>
using namespace std;

int g_Minus(int i, int j)
{
    return i - j;
}

int main()
{
    function<int(int, int)> f1 = bind(g_Minus, 1, 2);
    function<int()>         f2 = bind(g_Minus, 1, 2);                   // 绑定参数返回的函数对象实际等同这种形式
    function<int(int, int)> f3 = bind(g_Minus, placeholders::_1, placeholders::_2);
    function<int(int)>      f4 = bind(g_Minus, 1, placeholders::_1);    // 绑定第一个参数
    function<int(int)>      f5 = bind(g_Minus, placeholders::_1, 1);    // 绑定第二个参数

    cout << f1(3, 2) << endl;                                           // -1,实际传入参数将不起作用
    cout << f2()     << endl;                                           // -1
    cout << f3(3, 2) << endl;                                           // 1
    cout << f4(3)    << endl;                                           // -2
    cout << f5(3)    << endl;                                           // 2
return 1; }

• 绑定模板函数

#include <iostream>
#include <functional>
using namespace std;

template <class T>
T g_Minus(T i, T j)
{
    return i - j;
}

int main()
{
    function<int(int, int)> f1 = bind(g_Minus<int>, 1, 2);
    function<int()>         f2 = bind(g_Minus<int>, 1, 2);                  // 绑定参数返回的函数对象实际等同这种形式
    function<int(int, int)> f3 = bind(g_Minus<int>, placeholders::_1, placeholders::_2);
    function<int(int)>      f4 = bind(g_Minus<int>, 1, placeholders::_1);   // 绑定第一个参数
    function<int(int)>      f5 = bind(g_Minus<int>, placeholders::_1, 1);   // 绑定第二个参数

    cout << f1(3, 2) << endl;                                               // -1,实际传入参数将不起作用
    cout << f2()     << endl;                                               // -1
    cout << f3(3, 2) << endl;                                               // 1
    cout << f4(3)    << endl;                                               // -2
    cout << f5(3)    << endl;                                               // 2

    return 1;
}

• 绑定lambda表达式

#include <iostream>
#include <functional>
using namespace std;

int main()
{
    function<int(int, int)> f = bind([](int i, int j){ return i - j; }, placeholders::_1, placeholders::_2);
    cout << f(2, 3) << endl;                                            // -1
    return 1;
}

• 绑定函数对象

#include <iostream>
#include <functional>
using namespace std;

struct Minus
{
    int operator() (int i, int j)
    {
        return i - j;
    }
};

int main()
{
    function<int(int, int)> f = bind(Minus(), placeholders::_1, placeholders::_2);
    cout << f(2, 3) << endl;                                            // -1
    return 1;
}

• 绑定类静态成员函数

#include <iostream>
#include <functional>
using namespace std;

class Math
{
public:
    static int Minus(int i, int j)
    {
        return i - j;
    }
};

int main()
{
    function<int(int, int)> f = bind(&Math::Minus, placeholders::_1, placeholders::_2);
    cout << f(2, 3) << endl;                                            // -1
    return 1;
}

• 绑定类对象成员函数

#include <iostream>
#include <functional>
using namespace std;

class Math
{
public:
    int Minus(int i, int j)
    {
        return i - j;
    }
};

int main()
{
    Math m;
    function<int(int, int)> f = bind(&Math::Minus, &m, placeholders::_1, placeholders::_2);
    cout << f(2, 3) << endl;                                            // -1
    return 1;
}

• 返回值的类型转换

#include <iostream>
#include <functional>
using namespace std;

class Math
{
public:
    int Minus(int i, int j)
    {
        return i - j;
    }
};

struct Result
{
    int m_Result;

    Result() : m_Result(0) {}
    Result(int result) : m_Result(result) {}
};

int main()
{
    Math m;
    auto   f = bind<Result>(&Math::Minus, &m, placeholders::_1, placeholders::_2);
    Result r = f(2, 3);
    cout << r.m_Result << endl;                                         // -1
    return 1;
}
原文地址:https://www.cnblogs.com/heartchord/p/5032409.html