C++11中萬能的可調用類型聲明std::function

C++11中,callable object 包括傳統C函數,C++成員函數,函數對象(實現了()運算符的類的實例),lambda表達式(特殊函數對象)共4種。程序設計,特別是程序庫設計時,經常需要涉及到回調,如果針對每種不同的callable object單獨進行聲明類型,代碼將會非常散亂,也不靈活。如下示例:

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

// 傳統C函數
int c_function(int a, int b)
{
    return a + b;
}

// 函數對象
class Functor
{
public:
    int operator()(int a, int b)
    {
        return a + b;
    }
};

int main(int argc, char** argv)
{
    int(*f)(int, int);    // 聲明函數類型,賦值只能是函數指針
    f = c_function;
    cout << f(3, 4) << endl;

    Functor ff = Functor(); // 聲明函數對象類型,賦值只能是函數對象
    cout << ff(3, 4) << endl;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

幸運的是,C++標準庫的頭文件裏定義了std::function<>模板,此模板可以容納所有類型的callable object.示例代碼如下:

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

// 傳統C函數
int c_function(int a, int b)
{
    return a + b;
}

// 函數對象
class Functor
{
public:
    int operator()(int a, int b)
    {
        return a + b;
    }
};

int main(int argc, char** argv)
{
    // 萬能可調用對象類型
    std::function<int(int, int)> callableObject;

    // 可以賦值爲傳統C函數指針
    callableObject = c_function;
    cout << callableObject(3, 4) << endl;

    // 可以賦值爲函數對象
    Functor functor;
    callableObject = functor;
    cout << callableObject(3, 4) << endl;

    // 可以賦值爲lambda表達式(特殊函數對象)
    callableObject = [](int a, int b){
        return a + b;
    };
    cout << callableObject(3, 4) << endl;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41

std::function<>的這種多態能力確實很強,這樣可以定義一個回調列表,而列表的元素可接受的可調用物類型並不相同。如下:

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

// 傳統C函數
int c_function(int a, int b)
{
    return a + b;
}

// 函數對象
class Functor
{
public:
    int operator()(int a, int b)
    {
        return a + b;
    }
};

int main(int argc, char** argv)
{
    Functor functor;
    std::list<std::function<int(int, int)>> callables;

    callables.push_back(c_function);
    callables.push_back(functor);
    callables.push_back([](int x, int y)->int{
        return x + y;
    });

    for (const auto& e : callables)
    {
        cout << e(3, 4) << endl;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38

對於使用C回調機制的程序庫來說,C++的std::function<>能兼容傳統C函數指針,所以庫這一端使用std::function<>代替函數指針,並不會影響舊有客戶端程序的編碼方式。

轉載地址:http://blog.csdn.net/smstong/article/details/44958833

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