• std::function


    • 类模板声明

    复制代码
    // MS C++ 2013
    template<class _Fty> class function;
    template<class _Fty> class function : public _Get_function_impl<_Fty>::type { ... }
    
    // GCC 4.8.2
    template<typename _Signature>                  class function;
    template<typename _Res, typename... _ArgTypes> class function<_Res(_ArgTypes...)>
        : public _Maybe_unary_or_binary_function<_Res, _ArgTypes...>, private _Function_base { ... }

    // cplusplus.com
    template <class T> function; // undefined template <class Ret, class... Args> class function<Ret(Args...)>;
    复制代码

    • 类模板说明

           std::function是一个函数包装器模板,最早来自boost库,对应其boost::function函数包装器。在c++0x11中,将boost::function纳入标准库中。该函数包装器模板能包装任何类型的可调用元素(callable element),例如普通函数和函数对象。包装器对象可以进行拷贝,并且包装器类型仅仅只依赖于其用特征(call signature),而不依赖于可调用元素自身的类型。

           一个std::function类型对象实例可以包装下列这几种可调用元素类型:函数、函数指针、类成员函数指针或任意类型的函数对象(例如定义了operator()操作并拥有函数闭包)。std::function对象可被拷贝和转移,并且可以使用指定的调用特征来直接调用目标元素。当std::function对象未包裹任何实际的可调用元素,调用该std::function对象将抛出std::bad_function_call异常

    • 模板参数说明

           以cplusplus.com中描述的原型说明:

           T      : 通用类型,但实际通用类型模板并没有被定义,只有当T的类型为形如Ret(Args...)的函数类型才能工作。

           Ret   : 调用函数返回值的类型。

           Args : 函数参数类型。

    std::function详解


    • 包装普通函数

    复制代码
    #include <iostream>
    #include <functional>
    using namespace std;
    
    int g_Minus(int i, int j)
    {
        return i - j;
    }
    
    int main()
    {
        function<int(int, int)> f = g_Minus;
        cout << f(1, 2) << endl;                                            // -1
        return 1;
    }
    复制代码

    • 包装模板函数

    复制代码
    #include <iostream>
    #include <functional>
    using namespace std;
    
    template <class T>
    T g_Minus(T i, T j)
    {
        return i - j;
    }
    
    int main()
    {
        function<int(int, int)> f = g_Minus<int>;
        cout << f(1, 2) << endl;                                            // -1
        return 1;
    }
    复制代码

    • 包装lambda表达式

    复制代码
    #include <iostream>
    #include <functional>
    using namespace std;
    
    auto g_Minus = [](int i, int j){ return i - j; };
    
    int main()
    {
        function<int(int, int)> f = g_Minus;
        cout << f(1, 2) << endl;                                            // -1
        return 1;
    }
    复制代码

    • 包装函数对象

           非模板类型:

    复制代码
    #include <iostream>
    #include <functional>
    using namespace std;
    
    struct Minus
    {
        int operator() (int i, int j)
        {
            return i - j;
        }
    };
    
    int main()
    {
        function<int(int, int)> f = Minus();
        cout << f(1, 2) << endl;                                            // -1
        return 1;
    }
    复制代码

           模板类型:

    复制代码
    #include <iostream>
    #include <functional>
    using namespace std;
    
    template <class T>
    struct Minus
    {
        T operator() (T i, T j)
        {
            return i - j;
        }
    };
    
    int main()
    {
        function<int(int, int)> f = Minus<int>();
        cout << f(1, 2) << endl;                                            // -1
        return 1;
    }
    复制代码

    • 包装类静态成员函数

           非模板类型:

    复制代码
    #include <iostream>
    #include <functional>
    using namespace std;
    
    class Math
    {
    public:
        static int Minus(int i, int j)
        {
            return i - j;
        }
    };
    
    int main()
    {
        function<int(int, int)> f = &Math::Minus;
        cout << f(1, 2) << endl;                                            // -1
        return 1;
    }
    复制代码

           模板类型:

    复制代码
    #include <iostream>
    #include <functional>
    using namespace std;
    
    class Math
    {
    public:
        template <class T>
        static T Minus(T i, T j)
        {
            return i - j;
        }
    };
    
    int main()
    {
        function<int(int, int)> f = &Math::Minus<int>;
        cout << f(1, 2) << endl;                                            // -1
        return 1;
    }
    复制代码

    • 包装类对象成员函数

           非模板类型:

    复制代码
    #include <iostream>
    #include <functional>
    using namespace std;
    
    class Math
    {
    public:
        int Minus(int i, int j)
        {
            return i - j;
        }
    };
    
    int main()
    {
        Math m;
        function<int(int, int)> f = bind(&Math::Minus, &m, placeholders::_1, placeholders::_2);
        cout << f(1, 2) << endl;                                            // -1
        return 1;
    }
    复制代码

           模板类型:

    复制代码
    #include <iostream>
    #include <functional>
    using namespace std;
    
    class Math
    {
    public:
        template <class T>
        T Minus(T i, T j)
        {
            return i - j;
        }
    };
    
    int main()
    {
        Math m;
        function<int(int, int)> f = bind(&Math::Minus<int>, &m, placeholders::_1, placeholders::_2);
        cout << f(1, 2) << endl;                                            // -1
        return 1;
    }
  • 相关阅读:
    第九周作业
    第八周
    第七周
    Jmeter连接到Mysql
    数据库常用链接URL写法
    功能测试方法
    常建输入框的测试
    系统业务流程测试(转)
    Linux
    搭建Git服务器
  • 原文地址:https://www.cnblogs.com/lzjsky/p/15921493.html
Copyright © 2020-2023  润新知