- std::function是可調用對象的包裝器;std::bind是將可點用對象和其參數一起進行綁定,且綁定后的結果可以使用std::function對象進行保存,並延遲調用到需要調用的時候;
- 在C++中,可調用實體主要包括函數,函數指針,函數引用,可以隱式轉換為函數指定的對象,或者實現了opetator()的對象(即C++98中的functor)。C++0x中,新增加了一個std::function對象,std::function對象是對C++中現有的可調用實體的一種類型安全的包裹(我們知道像函數指針這類可調用實體,是類型不安全的)。
- bind是這樣一種機制,它可以預先把指定可調用實體的某些參數綁定到已有的變量,產生一個新的可調用實體,這種機制在回調函數的使用過程中也頗為有用。C++98中,有兩個函數bind1st和bind2nd,它們分別可以用來綁定functor的第一個和第二個參數,它們都是只可以綁定一個參數。各種限制,使得bind1st和bind2nd的可用性大大降低。C++0x中,提供了std::bind,它綁定的參數的個數不受限制,綁定的具體哪些參數也不受限制,由用戶指定,這個bind才是真正意義上的綁定,有了它,bind1st和bind2nd就沒啥用武之地了,因此C++0x中不推薦使用bind1st和bind2nd了,都是deprecated了。
-
C++11 的 lambda 表達式規范如下:
[
capture]
(
params)
mutable exception attribute->
ret{
body}
(1) [
capture]
(
params)
->
ret{
body}
(2) [
capture]
(
params)
{
body}
(3) [
capture]
{
body}
(4) 其中
- (1) 是完整的 lambda 表達式形式,
- (2) const 類型的 lambda 表達式,該類型的表達式不能改捕獲("capture")列表中的值。
- (3)省略了返回值類型的 lambda 表達式,但是該 lambda 表達式的返回類型可以按照下列規則推演出來:
- 如果 lambda 代碼塊中包含了 return 語句,則該 lambda 表達式的返回類型由 return 語句的返回類型確定。
- 如果沒有 return 語句,則類似 void f(...) 函數。
- 省略了參數列表,類似於無參函數 f()。
mutable 修飾符說明 lambda 表達式體內的代碼可以修改被捕獲的變量,並且可以訪問被捕獲對象的 non-const 方法。
exception 說明 lambda 表達式是否拋出異常(
noexcept
),以及拋出何種異常,類似於void f() throw(X, Y)。attribute 用來聲明屬性。
另外,capture 指定了在可見域范圍內 lambda 表達式的代碼內可見得外部變量的列表,具體解釋如下:
[a,&b]
a變量以值的方式唄捕獲,b以引用的方式被捕獲。[this]
以值的方式捕獲 this 指針。[&]
以引用的方式捕獲所有的外部自動變量。[=]
以值的方式捕獲所有的外部自動變量。[]
不捕獲外部的任何變量。
此外,params 指定 lambda 表達式的參數。
- 測試用例:
1、std::function作為回調函數使用示例
class A { std::function<void()> callback_; public: A(const std::function<void()>& f) :callback_(f){} void Notify() { callback_(); } }; class Foo { public: void operator()(void) { std::cout << __FUNCTION__ << std::endl;//get the func name } }; void Test8() { Foo foo; A a(foo); a.Notify(); }
2、std::function 和std::bind 結合使用:
void call_when_event(int x, const std::function<void(int)>& func) { if (!(x & 1)) { //cout << x << endl; func(x); } } void call_when_event1(int x,int y, const std::function<void(int,int)>& func) { if (!(x & 1)) { //cout << x << endl; func(x,y); } } void output(int x) { cout << x << endl; } void output_add_2(int x,int y) { cout << x + y << endl; } void Test7() { auto fr = std::bind(output, std::placeholders::_1); for (int i = 0; i < 10; i++) { call_when_event(i, fr); } std::cout << std::endl; auto fr1 = std::bind(output_add_2, std::placeholders::_1,std::placeholders::_2); for (int i = 0; i < 10; i++) { call_when_event1(i, i,fr1); } }
3、lambda 測試用例:
返回vector中大於5小於10的元素的個數:
void Test5() { std::vector<int> coll; for (int i = 0; i <= 10; i++) { coll.push_back(i); } int count = std::count_if(coll.begin(), coll.end(), [](int x){return x > 5 && x < 10; }); std::cout << count << std::endl; }