• C++ std::function的用法


    类模版std::function是一种通用、多态的函数封装。std::function的实例可以对任何可以调用的目标实体进行存储、复制、和调用操作,这些目标实体包括普通函数、Lambda表达式、函数指针、以及其它函数对象等。std::function对象是对C++中现有的可调用实体的一种类型安全的包裹(我们知道像函数指针这类可调用实体,是类型不安全的)。

    通常std::function是一个函数对象类,它包装其它任意的函数对象,被包装的函数对象具有类型为T1, …,TN的N个参数,并且返回一个可转换到R类型的值。std::function使用 模板转换构造函数接收被包装的函数对象;特别是,闭包类型可以隐式地转换为std::function。

    如下文中的

    typedef std::function<int(int)> Functional;
    

    最外层的int为返回值类型,里层int为参数类型
    封装普通函数例子:

    #include <iostream>
    #include <vector>
    #include <list>
    #include <map>
    #include <set>
    #include <string>
    #include <algorithm>
    #include <functional>
    #include <memory>
    
    using namespace std;
    
    typedef std::function<int(int)> Functional;
    
    int TestFunc(int a)  
    {  
        return a;  
    }
    
    int main()
    {
        Functional obj = TestFunc;    
        int res = obj(1);
        std::cout << res << std::endl;
    
        while(1);
        return 0;
    }
    
    

    封装lambda表达式 :

    #include <iostream>
    #include <vector>
    #include <list>
    #include <map>
    #include <set>
    #include <string>
    #include <algorithm>
    #include <functional>
    #include <memory>
    
    using namespace std;
    
    typedef std::function<int(int)> Functional;
    
    auto lambda = [](int a)->int{return a;};
    
    int main()
    {
        Functional obj = lambda;    
        res = obj(2);
        std::cout << res << std::endl;
    
        while(1);
        return 0;
    }
    
    

    封装仿函数:

    #include <iostream>
    #include <vector>
    #include <list>
    #include <map>
    #include <set>
    #include <string>
    #include <algorithm>
    #include <functional>
    #include <memory>
    
    using namespace std;
    
    typedef std::function<int(int)> Functional;
    
    class Functor
    {
    public:
        int operator()(int a)
        {
            return a;
        }
    };
    
    int main()
    {
        Functor func;
        Functional obj = func;
        res = obj(3);
        std::cout << res << std::endl;
    
        while(1);
        return 0;
    }
    
    

    封装类的成员函数和static成员函数 :

    #include <iostream>
    #include <vector>
    #include <list>
    #include <map>
    #include <set>
    #include <string>
    #include <algorithm>
    #include <functional>
    #include <memory>
    
    using namespace std;
    
    typedef std::function<int(int)> Functional;
    
    class CTest
    {
    public:
        int Func(int a)
        {
            return a;
        }
        static int SFunc(int a)
        {
            return a;
        }
    };
    
    int main()
    {
        CTest t;  
        Functional obj = std::bind(&CTest::Func, &t, std::placeholders::_1);  
        res = obj(3);  
        cout << "member function : " << res << endl;  
      
        obj = CTest::SFunc;  
        res = obj(4);  
        cout << "static member function : " << res << endl;  
    
        while(1);
        return 0;
    }
    
    

    关于可调用实体转换为std::function对象需要遵守以下两条原则:
    1、转换后的std::function对象的参数能转换为可调用实体的参数;
    2、可调用实体的返回值能转换为std::function对象的返回值。

    std::function对象最大的用处就是在实现函数回调,使用者需要注意,它不能被用来检查相等或者不相等,但是可以与NULL或者nullptr进行比较。

    为什么要用std::function?
    好用并实用的东西才会加入标准的。因为好用,实用,我们才在项目中使用它。std::function实现了一套类型消除机制,可以统一处理不同的函数对象类型。以前我们使用函数指针来完成这些;现在我们可以使用更安全的std::function来完成这些任务。

  • 相关阅读:
    SED&AWK
    load average[zhuan]
    To be learned
    Android计时器 android.widget.Chronometer
    Play初识
    获取视图的宽高
    自定义摄像机
    Android VideoView使用小记
    在android中,如何去掉webview读取网页后点击网页上的按钮出现的方框
    阿里云主机试用之自建站点和ftp上传所遇的2个问题
  • 原文地址:https://www.cnblogs.com/xiaohai123/p/16369781.html
Copyright © 2020-2023  润新知