C++ std::function函数包装器

0.前言

类模板 std::function 是通用多态函数封装器。 std::function 的实例能存储、复制及调用任何可调用对象。C++语言中有多种可调用对象:函数、函数指针、lambda表达式、bind创建的对象以及重载了函数调用运算符的类(仿函数)等。

和其他对象一样,可调用对象也有类型。如:每个lambda有它自己唯一的(未命名)类类型;函数及函数指针的类型则由其返回值类型和实参类型决定。然而,不同类型的可调用对象可能共享同一种调用形式。调用形式指明了返回的类型以及传递给调用的实参类型。一种调用形式对应一个函数(function)类型,如:

int(int,int)

是一个函数类型,它接受两个int,返回一个int。

1.使用 std::function 包装可调用对象

通过std::function,我们可以把可调用对象包装为更通用的形式。参照《C++ Primer》上计算器的例子:

#include <iostream>
#include <map>
#include <string>
#include <functional>

//函数
int add(int a, int b) { return a + b; }
//lambda
auto mod = [](int a, int b) { return a % b; };
//仿函数
struct divide
{
	int operator()(int a, int b) { return a / b; }
};


int main()
{
	using namespace std;
	map<string, function<int(int, int)>> binops{
		{"+",add}, //函数指针
		{"-",std::minus<int>()}, //标准库函数对象
		{"/",divide()}, //用户定义的函数对象
		{"*",[](int a,int b) { return a * b; }}, //未命名的lambda
		{"%",mod} //命名的lambda
	};

	cout << "3+2=" << binops["+"](3, 2) << endl;
	cout << "3-2=" << binops["-"](3, 2) << endl;
	cout << "3/2=" << binops["/"](3, 2) << endl;
	cout << "3*2=" << binops["*"](3, 2) << endl;
	cout << "3%2=" << binops["%"](3, 2) << endl;

	system("pause");
	return 0;
}

对于重载函数,我们不能直接将重载函数的名字存入function类型的对象中,可用函数指针或lambda来解决。

//重载的函数
int add(int a, int b) { return a + b; }
std::string add(std::string a, std::string b) { return a + b; }
//add_p接收int版本的函数
int (*add_p)(int, int) = add;
binops.insert({ "+",add_p });
//或者使用lambda
binops.insert({ "+",[](int a,int b) { return add(a,b); } });

2.其他示例

参照在线手册的例子:

#include <functional>
#include <iostream>
 
struct Foo {
    Foo(int num) : num_(num) {}
    void print_add(int i) const { std::cout << num_+i << '\n'; }
    int num_;
};
 
void print_num(int i)
{
    std::cout << i << '\n';
}
 
struct PrintNum {
    void operator()(int i) const
    {
        std::cout << i << '\n';
    }
};
 
int main()
{
    // 存储自由函数
    std::function<void(int)> f_display = print_num;
    f_display(-9);
 
    // 存储 lambda
    std::function<void()> f_display_42 = []() { print_num(42); };
    f_display_42();
 
    // 存储到 std::bind 调用的结果
    std::function<void()> f_display_31337 = std::bind(print_num, 31337);
    f_display_31337();
 
    // 存储到成员函数的调用
    std::function<void(const Foo&, int)> f_add_display = &Foo::print_add;
    const Foo foo(314159);
    f_add_display(foo, 1);
    f_add_display(314159, 1);
 
    // 存储到数据成员访问器的调用
    std::function<int(Foo const&)> f_num = &Foo::num_;
    std::cout << "num_: " << f_num(foo) << '\n';
 
    // 存储到成员函数及对象的调用
    using std::placeholders::_1;
    std::function<void(int)> f_add_display2 = std::bind( &Foo::print_add, foo, _1 );
    f_add_display2(2);
 
    // 存储到成员函数和对象指针的调用
    std::function<void(int)> f_add_display3 = std::bind( &Foo::print_add, &foo, _1 );
    f_add_display3(3);
 
    // 存储到函数对象的调用
    std::function<void(int)> f_display_obj = PrintNum();
    f_display_obj(18);
}

3.参考

参考书籍:《C++ Primer》中文第五版

在线文档:https://zh.cppreference.com/w/cpp/utility/functional/function

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章