C++11 (三) - std::function、std::bind、std::ref


一、std::bind

std::bind 预先把一个可调用实体的某些参数绑定到已有的变量,然后产生一个新的可调用实体,可以直接赋给 std::function 对象
std::bind 实际上是一种延迟计算的思想,将可调用对象保存起来,然后在需要的时候再调用,而且参数绑定支持占位符,std::placeholders::_1 代表这个位置将在函数调用时,被传入的第一个参数所替代

#include <iostream>
#include <functional>
using namespace std;
using namespace std::placeholders;    // std::placeholders::_1, _2, _3 ...

void func(int x, int y)
{
    cout << x << " " << y << endl;
}

int main()
{
    std::bind(func, 1, 2)();         // 1 2
    std::bind(func, _1, 2)(1);       // 1 2
    std::bind(func, _1, _2)(1, 2);   // 1 2
    std::bind(func, _2, _1)(1, 2);   // 2 1
    std::bind(func, 2, _1)(1);       // 2 1
    std::bind(func, 2, _2)(1, 2);    // 2 2
    // bind(func, 2, _2)(1); // err, 执行可调用实体时没有传入第二个参数

    return 0;
}

二、std::function

std::function 对 C++ 中各种可调用实体(普通函数、Lambda表达式、函数指针、仿函数以及其它函数对象等)进行封装,形成可调用的、统一的 std::function 对象

#include <functional>
#include <iostream>

int TestFunc(int a) { return a; } // 普通函数
auto lambdaFn = [](int a)->int { return a; }; // Lambda 表达式

class Functor // 仿函数 functor
{
public:
	int operator()(int a) { return a; }
};

class TestClass 
{
public:
	int ClassMemberFunc(int a) { return a; } // 类成员函数
	static int StaticMemberFunc(int a) { return a; } // 类静态函数
};

int main()
{
	std::function<int(int)> Functional;

	// 普通函数
	Functional = TestFunc;
	int result = Functional(10);
	std::cout << "普通函数:" << result << std::endl;

	// Lambda 表达式
	Functional = lambdaFn;
	result = Functional(20);
	std::cout << "Lambda 表达式:" << result << std::endl;

	// 仿函数
	Functional = Functor();
	result = Functional(30);
	std::cout << "仿函数:" << result << std::endl;

	// 类成员函数
	TestClass testObj;
	Functional = std::bind(&TestClass::ClassMemberFunc, testObj, std::placeholders::_1);
	result = Functional(40);
	std::cout << "普通成员函数:" << result << std::endl;

	// 类静态函数
	Functional = TestClass::StaticMemberFunc;
	result = Functional(50);
	std::cout << "静态成员函数:" << result << std::endl;

	return 0;
}

三、std::ref

std::bind 总是使用值拷贝的形式传参,哪怕函数声明为引用,可以使用 std::ref 来传引用。注意如果函数本身为值类型参数,即便使用了 std::ref 传引用,则传递的仍然是值而不是引用

#include <iostream>
#include <functional>
 
void fun(int& _a, int& _b, int _c)
{
	_a++;
	_b++;
	_c++; 
	std::cout << "in fun a:" << _a << " b:" << _b << " c:" << _c << std::endl;
}
 
int main()
{
	int a = 1, b = 1, c = 1;
	auto b_fun = std::bind(fun, a, std::ref(b), std::ref(c));
	b_fun();
 
	std::cout << "after fun a:" << a << " b:" << b << " c:" << c << std::endl;
	return 0;
}

/* output:
in fun a:2 b:2 c:2
after fun a:1 b:2 c:1
*/
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值