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