参考资料


• 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.0\VC\include\functional
template <class _Fun, class... _Types>
inline _Bind<false, void, _Fun, _Types...>
bind(_Fun && _Fx, _Types &&... _Args)
{ // bind a function object
...
} template <class _Rx, class... _Ftypes, class... _Types>
inline _Bind<true, _Rx, _Rx (* const)(_Ftypes...), _Types...>
bind(_Rx (*_Pfx)(_Ftypes...), _Types&&... _Args)
{ // bind a function pointer
...
} template <class _Rx, class _Farg0, class... _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, , );
function<int()> f2 = bind(g_Minus, , ); // 绑定参数返回的函数对象实际等同这种形式
function<int(int, int)> f3 = bind(g_Minus, placeholders::_1, placeholders::_2);
function<int(int)> f4 = bind(g_Minus, , placeholders::_1); // 绑定第一个参数
function<int(int)> f5 = bind(g_Minus, placeholders::_1, ); // 绑定第二个参数 cout << f1(, ) << endl; // -1,实际传入参数将不起作用
cout << f2() << endl; // -1
cout << f3(, ) << endl; //
cout << f4() << endl; // -2
cout << f5() << endl; // 2
return ;
}

• 绑定模板函数

#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>, , );
function<int()> f2 = bind(g_Minus<int>, , ); // 绑定参数返回的函数对象实际等同这种形式
function<int(int, int)> f3 = bind(g_Minus<int>, placeholders::_1, placeholders::_2);
function<int(int)> f4 = bind(g_Minus<int>, , placeholders::_1); // 绑定第一个参数
function<int(int)> f5 = bind(g_Minus<int>, placeholders::_1, ); // 绑定第二个参数 cout << f1(, ) << endl; // -1,实际传入参数将不起作用
cout << f2() << endl; // -1
cout << f3(, ) << endl; //
cout << f4() << endl; // -2
cout << f5() << endl; // return ;
}

• 绑定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(, ) << endl; // -1
return ;
}

• 绑定函数对象

#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(, ) << endl; // -1
return ;
}

• 绑定类静态成员函数

#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(, ) << endl; // -1
return ;
}

• 绑定类对象成员函数

#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(, ) << endl; // -1
return ;
}

• 返回值的类型转换

#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() {}
Result(int result) : m_Result(result) {}
}; int main()
{
Math m;
auto f = bind<Result>(&Math::Minus, &m, placeholders::_1, placeholders::_2);
Result r = f(, );
cout << r.m_Result << endl; // -1
return ;
}

std::bind的更多相关文章

  1. C++11之std::function和std::bind

    std::function是可调用对象的包装器,它最重要的功能是实现延时调用: #include "stdafx.h" #include<iostream>// std ...

  2. std::bind和std::function

    std::bind 用于绑定一个函数,返回另外一种调用方式的函数对象 ,可以改变参数顺序 和个数,特别是在多线程的程序中,经常用它将函数进行包装,然后打包发送给工作线程,让工作线程去执行我们的任务. ...

  3. std::function,std::bind

    std::function 和 std::bind 标准库函数bind()和function()定义于头文件中(该头文件还包括许多其他函数对象),用于处理函数及函数参数.bind()接受一个函数(或者 ...

  4. std::function,std::bind复习

    #include <iostream> #include <functional>//std::bind返回函数对象 void fun1(int a, int b) { std ...

  5. C++11 std::bind std::function 高级使用方法

    从最基础的了解,std::bind和std::function /* * File: main.cpp * Author: Vicky.H * Email: eclipser@163.com */ # ...

  6. C++ 11 笔记 (四) : std::bind

    std::bind 接受一个可调用的对象,一般就是函数呗.. 还是先上代码: void func(int x, int y, int z) { std::cout << "hel ...

  7. cocos2dx 3.0 它 使用std::bind更换CC_CALLBACK_N

    在cocos2dx 3.0 版本号,回调函数本质4一个CC_CALLBACK_N 替换功能.N的回调函数的参数的数量的代表 1.让我们来看看这些CC_CALLBACK_N怎么用 比方action的回调 ...

  8. std::bind 的使用说明

    转自: https://www.cnblogs.com/cmranger/p/4743926.html ///////////////////// std::bind bind是对C++98标准中函数 ...

  9. std::bind学习

    std::bind bind是对C++98标准中函数适配器bind1st/bind2nd的泛化和增强,可以适配任意的可调用对象,包括函数指针.函数引用.成员函数指针和函数对象. bind接受的第一个参 ...

  10. 第12课 std::bind和std::function(3)_std::function可调用对象包装器

    1. std::function (1)首先是一个类模板,用于包装可调用对象.可以容纳除了类成员(函数)指针之外的所有可调用对象. (2)可以将普通函数,lambda表达式和函数对象类统一起来.尽管它 ...

随机推荐

  1. AD smart pdf 中文丢失

    Altium Designer将原理图通过smart pdf导出,原理图中的中文丢失了. 将原理图中的所有中文字体改为宋体即可. 百度知道上的也有说: 打开软件后,点击左上角的[DXP]→[Prefe ...

  2. php -- 魔术方法 之 自动加载:__autoload()

    自动加载类 背景: 很多开发者写面向对象的应用程序时对每个类的定义建立一个 PHP 源文件.一个很大的烦恼是不得不在每个脚本开头写一个长长的包含文件列表(每个类一个文件). 在 PHP 5 中,不再需 ...

  3. PHP时间戳 strtotime()使用方法和技巧

    在php中我想要获取时间戳有多种方法,最常用的就是使用time函数与strtotime()函数把日期转换成时间戳了, 下面我来给大家分享一下时间戳函数 strtotime用法. 获取指定的年月日转化为 ...

  4. freemarker3

    结束标签 可以在结束标签中忽略user_def_dir_exp 也就是说可以写</@>来代替</@anything> 循环变量 <@myRepeatMacro count ...

  5. 转载 -- iOS中SDK的简单封装与使用

    一.功能总述 在博客开始的第一部分,我们先来看一下我们最终要实现的效果.下图中所表述的就是我们今天博客中要做的事情,下方的App One和App Two都植入了我们将要封装的LoginSDK, 两个A ...

  6. 剑指 offer set 21 圆圈中最后剩下的数字

    思路 1. 经典解法是用环形链表模拟圆圈, 然后每次减少一个节点. 时间复杂度为 o(mn), 空间复杂度为 o(n) 2. 转化成数学问题, 递推公式决定下一个元素. 时间复杂度为 o(n), 空间 ...

  7. THINKPHP5获取设置缓存的例子

    在THINKPHP5中 缓存的配置放在了config.php文件中 代码如下 如何设置缓存? 可以使用静态方法 Cache::set('key',$value,3600);//存储缓存 Cache:: ...

  8. HTML5游戏制作完全指南

    简介 创建画布 游戏循环 Hello world 创建player 键盘控制 a:使用jQuery Hotkeys b:移动player 添加更多游戏元素 炮弹 敌人 使用图片 碰撞检测 声音 简介 ...

  9. M451例程讲解之按键

    /**************************************************************************//** * @file main.c * @ve ...

  10. 《C++ Primer Plus》学习笔记 第1章 预备知识

    第一章 预备知识C++在C语言的基础上添加了对"面向对象编程"的支持和对"泛型编程"的支持.类 —— 面向对象模板 —— 泛型编程1.1 C++简介1.2 C+ ...