std::bind: Each argument may either be bound to a value or be a placeholder:

(1)、If bound to a value, calling the returned function object will always use that value as argument;

(2)、If a placeholder, calling the returned function object forwards an argument passed to the call (the one whose order number is specified by the placeholder).

std::bind receives a pointer to a function (it also can be a lambda expression or a functor) and receives a list of parameters that pass it to the function. As result, bind returns a new function object with a different prototype because all the parameters of the function were already specified.

A placeholder is an object that specifies the number of parameter in the bound function that will be used to enter this parameter.

std::bind is a Standard Function Objects that acts as a Functional Adaptor i.e. it takes a function as input and returns a new function Object as an output with with one or more of the arguments of passed function bound or rearranged.

std::bind函数是用来绑定函数调用的某些参数的。std::bind它可以预先把指定可调用实体的某些参数绑定到已有的变量,产生一个新的可调用实体。它绑定的参数的个数不受限制,绑定的具体哪些参数也不受限制,由用户指定。

std::bind:(1)、将函数、成员函数和闭包转成function函数对象;(2)、将多元(n>1)函数转成一元函数或者(n-1)元函数。

std::bind本身是一种延迟计算的思想,它本身可以绑定普通函数、全局函数、静态函数、类静态函数甚至是类成员函数。无法使用std::bind绑定一个重载函数的参数,必须显式地指出需要绑定的重载函数的版本。成员函数区别于普通函数的一个特殊之处在于,其第一个参数必须是该类型的一个对象(或对象的指针或引用)。

std::bind接受一个函数(或者函数对象,或者任何你可以通过"(…)"符号调用的事物),生成一个其有某一个或多个函数参数被”绑定”或重新组织的函数对象。绑定的参数将会以值传递的方式传递给具体函数,占位符将会以引用传递。

std::bind需要注意事项:

(1)、bind预先绑定的参数需要传具体的变量或值进去,对于预先绑定的参数,是pass-by-value的;

(2)、对于不事先绑定的参数,需要传std::placeholders进去,从_1开始,依次递增。placeholder是pass-by-reference的;

(3)、bind的返回值是可调用实体,可以直接赋给std::function对象;

(4)、对于绑定的指针、引用类型的参数,使用者需要保证在可调用实体调用之前,这些参数是可用的;

(5)、类的this可以通过对象或者指针来绑定。

下面是从其他文章中copy的测试代码,详细内容介绍可以参考对应的reference:

#include "bind.hpp"
#include <iostream>
#include <string>
#include <functional> // std::bind
#include <random>
#include <memory>
#include <algorithm>

//////////////////////////////////////////////////
// reference: http://en.cppreference.com/w/cpp/utility/functional/bind
static void f(int n1, int n2, int n3, const int& n4, int n5)
{
	std::cout << n1 << ' ' << n2 << ' ' << n3 << ' ' << n4 << ' ' << n5 << '\n';
}

static int g(int n1)
{
	return n1;
}

struct Foo_bind {
	void print_sum(int n1, int n2)
	{
		std::cout << n1 + n2 << '\n';
	}
	int data = 10;
};

int test_bind1()
{
	using namespace std::placeholders;  // for _1, _2, _3...

	// demonstrates argument reordering and pass-by-reference
	int n = 7;
	// (_1 and _2 are from std::placeholders, and represent future
	// arguments that will be passed to f1)
	auto f1 = std::bind(f, _2, _1, 42, std::cref(n), n);
	n = 10;
	f1(1, 2, 1001); // 1 is bound by _1, 2 is bound by _2, 1001 is unused
	// makes a call to f(2, 1, 42, n, 7)

	// nested bind subexpressions share the placeholders
	auto f2 = std::bind(f, _3, std::bind(g, _3), _3, 4, 5);
	f2(10, 11, 12);

	// common use case: binding a RNG with a distribution
	std::default_random_engine e;
	std::uniform_int_distribution<> d(0, 10);
	std::function<int()> rnd = std::bind(d, e); // a copy of e is stored in rnd
	for (int n = 0; n<10; ++n)
		std::cout << rnd() << ' ';
	std::cout << '\n';

	// bind to a pointer to member function
	Foo_bind foo;
	auto f3 = std::bind(&Foo_bind::print_sum, &foo, 95, _1);
	f3(5);

	// bind to a pointer to data member
	auto f4 = std::bind(&Foo_bind::data, _1);
	std::cout << f4(foo) << '\n';

	// smart pointers can be used to call members of the referenced objects, too
	//std::cout << f4(std::make_shared<Foo_bind>(foo)) << '\n'
	//	<< f4(std::make_unique<Foo_bind>(foo)) << '\n';

	return 0;
}

///////////////////////////////////////////////////
// reference: http://www.cplusplus.com/reference/functional/bind/
// a function: (also works with function object: std::divides<double> my_divide;)
double my_divide(double x, double y) { return x / y; }

struct MyPair {
	double a, b;
	double multiply() { return a*b; }
};

int test_bind2()
{
	using namespace std::placeholders;    // adds visibility of _1, _2, _3,...

	// binding functions:
	auto fn_five = std::bind(my_divide, 10, 2);               // returns 10/2
	std::cout << fn_five() << '\n';                          // 5

	auto fn_half = std::bind(my_divide, _1, 2);               // returns x/2
	std::cout << fn_half(10) << '\n';                        // 5

	auto fn_invert = std::bind(my_divide, _2, _1);            // returns y/x
	std::cout << fn_invert(10, 2) << '\n';                    // 0.2

	auto fn_rounding = std::bind<int>(my_divide, _1, _2);     // returns int(x/y)
	std::cout << fn_rounding(10, 3) << '\n';                  // 3

	MyPair ten_two{ 10, 2 };

	// binding members:
	auto bound_member_fn = std::bind(&MyPair::multiply, _1); // returns x.multiply()
	std::cout << bound_member_fn(ten_two) << '\n';           // 20

	auto bound_member_data = std::bind(&MyPair::a, ten_two); // returns ten_two.a
	std::cout << bound_member_data() << '\n';                // 10

	return 0;
}

/////////////////////////////////////////////////
// reference: https://oopscenities.net/2012/02/24/c11-stdfunction-and-stdbind/
static void show(const std::string& a, const std::string& b, const std::string& c)
{
	std::cout << a << "; " << b << "; " << c << std::endl;
}

int test_bind3()
{
	using namespace std::placeholders;

	// Thanks to the placeholders, you can change the order of the arguments passed as parameters to a bound function.
	auto x = bind(show, _1, _2, _3);
	auto y = bind(show, _3, _1, _2);
	auto z = bind(show, "hello", _2, _1);

	x("one", "two", "three");
	y("one", "two", "three");
	z("one", "two");

	return 0;
}

/////////////////////////////////////////////////////
// reference: http://stackoverflow.com/questions/22422147/why-is-stdbind-not-working-without-placeholders-in-this-example-member-functi
static void f_(int a, int b, int c)
{
	fprintf(stdout, "a = %d, b = %d, c = %d\n", a, b, c);
}

struct foo_bind
{
	void f() const { fprintf(stdout, "foo_bind\n"); };
};

int test_bind4()
{
	using namespace std::placeholders;

	// The first parameter of std::bind() is the function to be called, and the rest are the arguments of the call.
	std::function<void()> f_call = std::bind(f_, 1, 2, 3);
	f_call(); //Equivalent to f_(1,2,3)

	// If you need to specify that some parameters have to be specified at the call point,
	// you have to use placeholders to represent that parameters
	std::function<void(int, int, int)> f_call2 = std::bind(f_, _1, _2, _3);
	f_call2(1, 2, 3); //Same as f_(1,2,3)

	// Note that the numbers of the placeholders specify the number of the parameter at the call point.
	// The first parameter of the call point is identified by _1, the second by _2, and so on.
	// This could be used to specify parameters in different ways, reordering the parameters of a function call, etc.
	std::function<void(int, int)> f_call3 = std::bind(f_, _1, 2, _2);
	f_call3(1, 3); //Equivalent to f_( 1 , 2 , 3 );

	std::function<void(int, int, int)> reordered_call = std::bind(f_, _3, _2, _1);
	reordered_call(3, 2, 1); //Same as f_( 1 , 2 , 3 );

	// std::bind() could be used to bind a member function to the object used to call it
	foo_bind myfoo;
	std::function<void()> f = std::bind(&foo_bind::f, std::cref(myfoo));
	f();

	return 0;
}

/////////////////////////////////////////////////
// reference: https://msdn.microsoft.com/en-us/library/bb982702.aspx
static void square(double x)
{
	std::cout << x << "^2 == " << x * x << std::endl;
}

static void product(double x, double y)
{
	std::cout << x << "*" << y << " == " << x * y << std::endl;
}

int test_bind5()
{
	using namespace std::placeholders;

	double arg[] = { 1, 2, 3 };

	std::for_each(&arg[0], arg + 3, square);
	std::cout << std::endl;

	std::for_each(&arg[0], arg + 3, std::bind(product, _1, 2));
	std::cout << std::endl;

	std::for_each(&arg[0], arg + 3, std::bind(square, _1));

	return (0);
}

GitHubhttps://github.com/fengbingchun/Messy_Test

C++11中std::bind的使用的更多相关文章

  1. C++11中std::function的使用

    class template std::function is a general-purpose polymorphic function wrapper. Instances of std::fu ...

  2. c++11之std::bind简单使用

    note 更多用法,请参考: cppreference 用的少,容易忘. 我的理解 类似延迟计算. 比如,回调函数,将回调函数传入后,回调函数不一定马上被调用. 它是一个模板类,调用后将生成一个新的调 ...

  3. C++11中std::move的使用

    std::move is used to indicate that an object t may be "moved from", i.e. allowing the effi ...

  4. 第11课 std::bind和std::function(2)_std::bind绑定器

    1. 温故知新:std::bind1st和std::bind2nd (1)bind1st.bind2nd首先它们都是函数模板,用于将参数绑定到可调用对象(如函数.仿函数等)的第1个或第2个参数上. ( ...

  5. C++11中std::forward的使用 (转)

    std::forward argument: Returns an rvalue reference to arg if arg is not an lvalue reference; If arg ...

  6. C++11中std::unordered_map的使用

    unordered map is an associative container that contains key-value pairs with unique keys. Search, in ...

  7. C++11中std::forward的使用

    std::forward argument: Returns an rvalue reference to arg if arg is not an lvalue reference; If arg ...

  8. C++11中std::move、std::forward、左右值引用、移动构造函数的测试

    关于C++11新特性之std::move.std::forward.左右值引用网上资料已经很多了,我主要针对测试性能做一个测试,梳理一下这些逻辑,首先,左值比较熟悉,右值就是临时变量,意味着使用一次就 ...

  9. 用C++11的std::async代替线程的创建

    c++11中增加了线程,使得我们可以非常方便的创建线程,它的基本用法是这样的: void f(int n); std::thread t(f, n + 1); t.join(); 但是线程毕竟是属于比 ...

随机推荐

  1. python 提取字符串中的数字组成新的字符串

    方法一 # 有一个字符串text = "aAsmr3idd4bgs7Dlsf9eAF" # 请将text字符串中的数字取出,并输出成一个新的字符串 import re text = ...

  2. datatable Left and right fixed columns

    $(document).ready(function() { var table = $('#example').DataTable( { scrollY: "300px", sc ...

  3. codeforces 611D New Year and Ancient Prophecy

    f[i = 以i结尾][j = 长度为j] = 方案数. f[i][j] = sum{ f[i-j][k] , k < j || (k == j && s(i-j+1,j) &g ...

  4. NYOJ298 点的变换 【矩阵乘法经典】

    任意门:http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=298 点的变换 时间限制:2000 ms  |  内存限制:65535 KB 难度:5 ...

  5. Playrix Codescapes Cup (Codeforces Round #413, rated, Div. 1 + Div. 2) C. Fountains 【树状数组维护区间最大值】

    题目传送门:http://codeforces.com/contest/799/problem/C C. Fountains time limit per test 2 seconds memory ...

  6. 常用PowerShell命令

    查看版本: 文件重命名: 别名查看: 当前路径:(别名pwd) 切换路径:(别名cd) 子列表:(别名ls  -Force可查看隐藏项) 查看用户:(可显示隐藏) 资源管理器打开当前目录:(cmd亦可 ...

  7. 一步一步部署SSIS包图解教程

    本文就SQL统计分析SSIS包的部署进行一次详细的部署图解教程,Sql Server Integration Services 提供了非常简单的部署工具,利用这些工具可以方便地将包文件(*.dtsx) ...

  8. 【洛谷P2447】[SDOI2010]外星千足虫

    外星千足虫 题目链接 首先,它一看题解就是个高斯消元 只是改成了奇偶性的判断 性质: 奇数+奇数=偶数 奇数+偶数=奇数 偶数+奇数=奇数 偶数+偶数=偶数 于是我们用1表示奇数,0表示偶数 1+1= ...

  9. 【题解】P1516 青蛙的约会(Exgcd)

    洛谷P1516:https://www.luogu.org/problemnew/show/P1516 思路: 设两只青蛙跳了T步 则A的坐标为X+mT   B的坐标为Y+nT 要使他们相遇 则满足: ...

  10. 19.springboot邮件服务服务器部署访问不到邮箱服务器解决方案

    1.前言 在Springboot项目的生产环境中,win系统环境下,邮箱服务是可以正常使用的. 当项目部署到阿里云服务器上之后,因为服务器端口采用安全组的方式,25端口访问不到. 在网上查找了一部分资 ...