C++11:bind的使用

#include <iostream>
#include <functional>


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

int main()
{
	std::bind(func, 1, 2)();                     //输出:1 2
	std::bind(func, std::placeholders::_1, 2)(1);//输出:1 2

	using namespace std::placeholders;    // adds visibility of _1, _2, _3,...
	std::bind(func, 2, _1)(1);       //输出:2 1
	std::bind(func, 2, _2)(1, 2);    //输出:2 2
	std::bind(func, _1, _2)(1, 2);   //输出:1 2
	std::bind(func, _2, _1)(1, 2);    //输出:2 1

	//err, 调用时没有第二个参数
	//bind(func, 2, _2)(1);

	system("pause");
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41741165/article/details/83686782