C++的std::bind

一、介绍

C++11中提供了std::bind。bind()函数的意义就像它的函数名一样,是用来绑定函数调用的某些参数的。

bind的思想实际上是一种延迟计算的思想,将可调用对象保存起来,然后在需要的时候再调用。而且这种绑定是非常灵活的,不论是普通函数、函数对象、还是成员函数都可以绑定,而且其参数可以支持占位符。

std::bind函数有两种函数原型,定义如下:

template< class F, class... Args >
/*unspecified*/ bind( F&& f, Args&&... args );
 
template< class R, class F, class... Args >
/*unspecified*/ bind( F&& f, Args&&... args );

Parameters

f-Callable object (function object, pointer to function, reference to function, pointer to member function, or pointer to data member) that will be bound to some arguments
args-list of arguments to bind, with the unbound arguments replaced by the placeholders _1, _2, _3... of namespace std::placeholders

二、实例

这里要先学习仿函数。请参考仿函数的使用

实例1

#include <iostream>
#include <functional>
using namespace std;
 
int TestFunc(int a, char c, float f)
{
    cout << a << endl;
    cout << c << endl;
    cout << f << endl;
 
    return a;
}
 
int main()
{
    auto bindFunc1 = bind(TestFunc, std::placeholders::_1, 'A', 100.1);
    bindFunc1(10); //等于TestFunc(10,'A', 100.1)
 
    cout << "=================================\n";
 
    auto bindFunc2 = bind(TestFunc, std::placeholders::_2, std::placeholders::_1, 100.1);
    bindFunc2('B', 10); //等于TestFunc(10,'B', 100.1)
 
    cout << "=================================\n";
 
    auto bindFunc3 = bind(TestFunc, std::placeholders::_2, std::placeholders::_3, std::placeholders::_1);
    bindFunc3(100.1, 30, 'C'); //等于TestFunc(30,'C', 100.1)
 
    return 0;
}

 

上面这段代码主要说的是bind中std::placeholders的使用。 std::placeholders是一个占位符。当使用bind生成一个新的可调用对象时,std::placeholders表示新的可调用对象的第 几个参数和原函数的第几个参数进行匹配。

auto bindFunc3 = bind(TestFunc, std::placeholders::_2, std::placeholders::_3, std::placeholders::_1);
 
bindFunc3(100.1, 30, 'C');

可以看到,在bind的时候,第一个位置是TestFunc,除了这个,参数的第一个位置为占位符std::placeholders::_2,这就表示,调用bindFunc3的时候,它的第二个参数——即30,和TestFunc的第一个参数匹配,所以std::placeholders::_2为30,以此类推,最后,实际执行的是TestFunc(30,'C', 100.1)。 

实例2


#include <random>
#include <iostream>
#include <memory>
#include <functional>

void f(int n1, int n2, int n3, const int& n4, int n5)
{
	std::cout << n1 << ' ' << n2 << ' ' << n3 << ' ' << n4 << ' ' << n5 << '\n';
}

int g(int n1)
{
	return n1;
}

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

int main()
{
	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, 42, _1, 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, 42, 1, 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); // makes a call to f(12, g(12), 12, 4, 5);

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

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

	std::cout << f4(std::make_shared<Foo>(foo)) << '\n'
		<< f4(std::make_unique<Foo>(foo)) << '\n';

        return 0;
}

参考:

std::bind - cppreference.com

C++11新特性:参数绑定——std::bind____Blue_H的博客-CSDN博客_std::bind()

C++11中的std::bind__大猪的博客-CSDN博客_c++ std::bind

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C++中的std::bind是一个函数模板,用于创建一个可调用对象(函数对象或成员函数指针),并绑定其参数。它可以将一个函数或成员函数与一组参数绑定在一起,形成一个新的可调用对象。 std::bind的基本语法如下: ```cpp std::bind(Function, args...) ``` 其中,Function是要绑定的函数或成员函数指针,args是要绑定的参数。 使用std::bind可以实现以下功能: 1. 绑定普通函数的部分参数,生成一个新的可调用对象。 2. 绑定成员函数的对象指针和部分参数,生成一个新的可调用对象。 3. 绑定成员函数的对象引用和部分参数,生成一个新的可调用对象。 下面是一些示例: 1. 绑定普通函数的部分参数: ```cpp #include <iostream> #include <functional> void printSum(int a, int b) { std::cout << "Sum: " << a + b << std::endl; } int main() { auto printSumWith5 = std::bind(printSum, 5, std::placeholders::_1); printSumWith5(10); // 输出:Sum: 15 return 0; } ``` 2. 绑定成员函数的对象指针和部分参数: ```cpp #include <iostream> #include <functional> class MyClass { public: void printProduct(int a, int b) { std::cout << "Product: " << a * b << std::endl; } }; int main() { MyClass obj; auto printProductWith2 = std::bind(&MyClass::printProduct, &obj, std::placeholders::_1); printProductWith2(5); // 输出:Product: 10 return 0; } ``` 3. 绑定成员函数的对象引用和部分参数: ```cpp #include <iostream> #include <functional> class MyClass { public: void printDifference(int a, int b) { std::cout << "Difference: " << a - b << std::endl; } }; int main() { MyClass obj; auto printDifferenceWith3 = std::bind(&MyClass::printDifference, std::ref(obj), std::placeholders::_1); printDifferenceWith3(7); // 输出:Difference: 4 return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值