初学者的C++11学习:可调用对象及可调用对象包装器,绑定器

内容学习自:爱编程的大丙

一、可调用对象

先来了解一下什么是可调用对象,C++中一组执行任务的语句都可以视为一个函数,一个可调用对象。在程序设计的过程中,我们习惯于把那些具有复用性的一组语句抽象为函数,把变化的部分抽象为函数的参数。

  • 函数指针
  • operator()成员函数的类对象(仿函数)
  • 可被转换为函数指针的类对象
  • 类成员函数指针或者类成员指针
struct Test
{
    // ()操作符重载
    void operator()(string msg)
    {
        cout << "msg: " << msg << endl;
    }
static void print(int a, string b)
    {
        cout << "name: " << b << ", age: " << a << endl;
    }
     void print_1(int a, string b)
    {
        cout << "name: " << b << ", age: " << a << endl;
    }


    // 将类对象转换为函数指针
    operator func_ptr()
    {
        return print;
    }
private:
    int m_num;

};
/1.
int print(int a, double b)
{
    cout << a << b << endl;
    return 0;
}
// 定义函数指针
int (*func)(int, double) = &print;//func就是一个可调用对象


void main()
{
/2.
    Test t;
    t("我是要成为海贼王的男人!!!");	// 仿函数

/3.
    Test t1;
    t1(19,"Monkey D.Luffy");// 对象转换为函数指针, 并调用
/4.
    // 定义类成员函数指针指向类成员函数
    void (Test::*func_ptr)(int, string) = &Test::print_1;
    // 类成员指针指向类成员变量
    int Test::*obj_ptr = &Test::m_num;

    Test t;
    // 通过类成员函数指针调用类成员函数
    (t.*func_ptr)(19, "Monkey D. Luffy");
    // 通过类成员指针初始化类成员变量
    t.*obj_ptr = 1;
    cout << "number is: " << t.m_num << endl;

    return 0;
}


C++ 中的可调用类型虽然具有比较统一的操作形式,但定义方式五花八门,这样在我们试图使用统一的方式保存,或者传递一个可调用对象时会十分繁琐。现在,C++11通过提供std::function 和 std::bind统一了可调用对象的各种操作。

二、可调用对象包装器

std::function是可调用对象的包装器。它是一个类模板,可以容纳除了类成员(函数)指针之外的所有可调用对象。通过指定它的模板参数,它可以用统一的方式处理函数、函数对象、函数指针,并允许保存和延迟执行它们

头文件<functional>

//可调用对象包装器
int add_1(int a, int b)
{
	cout << a << " + " << b << " = " << a + b << endl;
	return a + b;
}

class test8
{
public:
	static int sub(int a, int b)
	{
		cout << a << " - " << b << " = " << a - b << endl;
		return a - b;
	}
};

class test9
{
public:
	int operator()(int a, int b)
	{
		cout << a << " * " << b << " = " << a * b << endl;
		return a * b;
	}
};
void main()
{
#if 1
	// 绑定一个普通函数
	function<int(int, int)> f1 = add_1;
	// 绑定以静态类成员函数
	function<int(int, int)> f2 = test8::sub;
	// 绑定一个仿函数
	test9 t;
	function<int(int, int)> f3 = t;

	// 函数调用
	f1(9, 3);
	f2(9, 3);
	f3(9, 3);

	return 0;
#endif
}

包装完成得到的对象相当于一个函数指针,和函数指针的使用方式相同,通过包装器对象就可以完成对包装的函数的调用了。

#include <iostream>
#include <functional>
using namespace std;

class test10
{
public:
	// 构造函数参数是一个包装器对象
	test10(const function<void()>& f) : callback(f)
	{
	}

	void notify()
	{
		callback(); // 调用通过构造函数得到的函数指针
	}
private:
	function<void()> callback;
};

class test11
{
public:
	void operator()()
	{
		cout << "我的梦想是成为火影!!!" << endl;
	}
};
int main(void)
{
    test11 b;
	test10 a(b); // 仿函数通过包装器对象进行包装
	a.notify();


    return 0;
}

使用对象包装器 std::function 可以非常方便的将仿函数转换为一个函数指针,通过进行函数指针的传递,在其他函数的合适的位置就可以调用这个包装好的仿函数了

三、绑定器

std::bind用来将可调用对象与其参数一起进行绑定。绑定后的结果可以使用std::function进行保存,并延迟调用到任何我们需要的时候。通俗来讲,它主要有两大作用:

  • 将可调用对象与其参数一起绑定成一个仿函数。
  • 将多元(参数个数为n,n>1)可调用对象转换为一元或者(n-1)元可调用对象,即只绑定部分参数。
void callFunc(int x, const function<void(int)>& f)
{
    if (x % 2 == 0)
    {
        f(x);
    }
}

void output(int x)
{
    cout << x << " ";
}

void output_add(int x)
{
    cout << x + 10 << " ";
}

int main(void)
{
    // 使用绑定器绑定可调用对象和参数
    auto f1 = bind(output, placeholders::_1);
    for (int i = 0; i < 10; ++i)
    {
        callFunc(i, f1);
    }
    cout << endl;

    auto f2 = bind(output_add, placeholders::_1);
    for (int i = 0; i < 10; ++i)
    {
        callFunc(i, f2);
    }
    cout << endl;

    return 0;
}

std::bind绑定器返回的是一个仿函数类型,得到的返回值可以直接赋值给一个std::function,在使用的时候我们并不需要关心绑定器的返回值类型,使用auto进行自动类型推导就可以了。

placeholders::_1 是一个占位符,代表这个位置将在函数调用时被传入的第一个参数所替代。同样还有其他的占位符 placeholders::_2、placeholders::_3、placeholders::_4、placeholders::_5 等……

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

void main()
{
 // 使用绑定器绑定可调用对象和参数, 并调用得到的仿函数
    bind(output, 1, 2)();
    bind(output, placeholders::_1, 2)(10);
    bind(output, 2, placeholders::_1)(10);

    // error, 调用时没有第二个参数
    // bind(output, 2, placeholders::_2)(10);
    // 调用时第一个参数10被吞掉了,没有被使用
    bind(output, 2, placeholders::_2)(10, 20);

    bind(output, placeholders::_1, placeholders::_2)(10, 20);
    bind(output, placeholders::_2, placeholders::_1)(10, 20);

}

再来看一个例子

class test12
{
public:
	void output(int x, int y)
	{
		cout << "x: " << x << ", y: " << y << endl;
	}
	int m_number = 100;
};

void main()
{
    test12 t;
	// 绑定类成员函数
	function<void(int, int)> f1 =
		bind(&test12::output, &t, placeholders::_1, placeholders::_2);
	// 绑定类成员变量(公共)
	function<int& (void)> f2 = bind(&test12::m_number, &t);

	// 调用
	f1(520, 1314);
	f2() = 2333;
	cout << "t.m_number: " << t.m_number << endl;
}

 

在用绑定器绑定类成员函数或者成员变量的时候需要将它们所属的实例对象一并传递到绑定器函数内部。f1的类型是function<void(int, int)>,通过使用std::bind将Test的成员函数output的地址和对象t绑定,并转化为一个仿函数并存储到对象f1中。

使用绑定器绑定的类成员变量m_number得到的仿函数被存储到了类型为function<int&(void)>的包装器对象f2中,并且可以在需要的时候修改这个成员。其中int是绑定的类成员的类型,并且允许修改绑定的变量,因此需要指定为变量的引用,由于没有参数因此参数列表指定为void。

示例程序中是使用 function 包装器保存了 bind 返回的仿函数,如果不知道包装器的模板类型如何指定,可以直接使用 auto 进行类型的自动推导,这样使用起来会更容易一些。

 

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值