包装器function,适配器bind

1, 包装器function

std::function模板类是一个通用的可调用对象的包装器,用简单的、统一的方式处理可调用对象。

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

// 普通函数
void show(int bh, const string& message) {
    cout << "亲爱的" << bh << "," << message << endl;
}

struct AA   // 类中有静态成员函数。
{
    static void show(int bh, const string& message) {
        cout << "亲爱的" << bh << "," << message << endl;
    }
};

struct BB   // 仿函数。
{
    void operator()(int bh, const string& message) {
        cout << "亲爱的" << bh << "," << message << endl;
    }
};

struct CC   // 类中有普通成员函数。
{
    void show(int bh, const string& message) {
        cout << "亲爱的" << bh << "," << message << endl;
    }
};

struct DD       // 可以被转换为普通函数指针的类。
{
    using Fun = void (*)(int, const string&);    // 函数指针的别名。
    operator Fun() {
        return show;    // 返回普通函数show的地址。
    }
};

int main() {
    // 普通函数
    using Fun = void(int, const string&);   // 函数类型的别名。
    void(*fp1)(int, const string&) = show;  // 声明函数指针,指向函数对象。
    fp1(1, "我是一只傻鸟");                   // 用函数指针调用普通函数。
    function<void(int, const string&)> fn1 = show; // 包装普通全局函数show。
    fn1(1, "我是一个傻鸟");                           // 用function对象调用普通全局函数show

    // 类的静态成员函数
    void(*fp2)(int, const string&) = AA::show; // 用函数指针指向类的静态成员函数
    fp2(2, "我是一只傻鸟");
    function<void(int, const string&)> fn2 = AA::show; // 包装类的静态成员函数。
    fn2(2, "我是一只傻鸟");                   // 用function对象调用类的静态成员函数。

    // 仿函数。
    BB bb;
    bb(3, "我是一只傻鸟");
    function<void(int, const string&)> fn3 = BB(); // 包装仿函数
    fn3(3, "我是一只傻鸟");

    // 创建lambda对象。
    auto lb = [](int bh, const string& message){
        std::cout << "亲爱的" << bh << "," << message << std::endl;
    };
    lb(4, "我是一只傻鸟"); // 调用lambda函数。
    function<void(int, const string&)> fn4 = lb; // 包装lamba函数。
    fn4(4, "我是一只傻鸟");       // 用function对象调用lamba函数

    // 类的非静态成员函数。
    CC cc;
    void (CC::*fp5)(int, const string&) = &CC::show; // 定义类成员函数的指针
    (cc.*fp5)(5, "我是一只傻鸟"); // 用类成员函数的指针调用类的成员函数
    function<void(CC&, int, const string&)> fn5 = &CC::show;    // 包装成员函数。
    fn5(cc, 5, "我是一只傻鸟");    // 用function对象调用成员函数

    // 可以被转换为函数指针的类对象。
    DD dd;
    dd(6, "我是一只傻傻鸟。");                       // 用可以被转换为函数指针的类对象调用普通函数。
    function<void(int, const string&)> fn6 = dd; // 包装可以被转换为函数指针的类。
    fn6(6, "我是一只傻傻鸟。");                  // 用function对象调用它。

    function<void(int, const string&)> fx = dd;
    try {
        if (fx) fx(7, "我是一只傻傻鸟。");
    }
    catch (std::bad_function_call e) {
        cout << "抛出了std::bad_function_call异常。";
    }

    std::cout << "hello world end" << std::endl;

    return 0;
}

2, 适配器bind 的基本用法

std::bind()模板函数是一个通用的函数适配器(绑定器),它用一个可调用对象及其参数,生成一个新的可调用对象,以适应模板

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

// 普通函数
void show(int bh, const string& message) {
    cout << "亲爱的" << bh << "号," << message << endl;
}

int main()
{
    function<void(int, const string&)> fn1 = show;
    function<void(int, const string&)> fn2 = bind(show, placeholders::_1, placeholders::_2);
    fn1(1, "我是一只傻傻鸟。");
    fn2(2, "我是一只傻傻鸟。");

    function<void(const string&, int)> fn3 = bind(show, placeholders::_2, placeholders::_1);
    fn3("我是一只傻傻鸟。", 3);
    function<void(const string&)> fn4 = bind(show, 4, placeholders::_1);
    fn4("我是一只傻傻鸟。");

    function<void(int, const string&,int)> fn5 = bind(show, placeholders::_1, placeholders::_2);
    fn5(5, "我是一只傻傻鸟。", 88);
    return 0;
}

2, 适配器bind 的绑定六种可调用对象

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

// 普通函数
void show(int bh, const string& message) {
    cout << "亲爱的" << bh << "," << message << endl;
}

struct AA   // 类中有静态成员函数。
{
    static void show(int bh, const string& message) {
        cout << "亲爱的" << bh << "," << message << endl;
    }
};

struct BB   // 仿函数。
{
    void operator()(int bh, const string& message) {
        cout << "亲爱的" << bh << "," << message << endl;
    }
};

struct CC   // 类中有普通成员函数。
{
    void show(int bh, const string& message) {
        cout << "亲爱的" << bh << "," << message << endl;
    }
};

struct DD       // 可以被转换为普通函数指针的类。
{
    using Fun = void (*)(int, const string&);    // 函数指针的别名。
    operator Fun() {
        return show;    // 返回普通函数show的地址。
    }
};

int main()
{
    // 普通函数。
    function<void(int, const string&)> fn1 = bind(show, placeholders::_1, placeholders::_2);    // 绑定普通全局函数show。
    fn1(1, "我是一只乌龟。");                                      // 用function对象调用普通全局函数show。

    // 类的静态成员函数。
    function<void(int, const string&)> fn2 = bind(AA::show, placeholders::_1, placeholders::_2);        // 绑定类的静态成员函数。
    fn2(2, "我是一只乌龟。");                                              // 用function对象调用类的静态成员函数。

    // 仿函数。
    function<void(int, const string&)> fn3 = bind(BB(), placeholders::_1, placeholders::_2);            // 绑定仿函数。
    fn3(3, "我是一只乌龟。");                                      // 用function对象调用仿函数。

    // 创建lambda对象。
    auto lb = [](int bh, const string& message) {
        cout << "亲爱的" << bh << "," << message << endl;
    };
    function<void(int, const string&)> fn4 = bind(lb, placeholders::_1, placeholders::_2);          // 绑定lamba函数。
    fn4(4, "我是一只乌龟。");                                      // 用function对象调用lamba函数。

    // 类的非静态成员函数。
    CC cc;
    //function<void(CC&, int, const string&)> fn11 = bind(&CC::show, placeholders::_1, placeholders::_2, placeholders::_3);     // 绑定成员函数。
    //fn11(cc, 5, "我是一只乌龟。");                                           // 用function对象调用成员函数。
    function<void(int, const string&)> fn5 = bind(&CC::show,&cc,placeholders::_1, placeholders::_2);        // 绑定成员函数。
    fn5(5, "我是一只乌龟。");                                          // 用function对象调用成员函数。

    // 可以被转换为函数指针的类对象。
    DD dd;
    function<void(int, const string&)> fn6 = bind(dd, placeholders::_1, placeholders::_2);          // 绑定可以被转换为函数指针的类。
    fn6(6, "我是一只乌龟。");                                      // 用function对象调用它。
    return 0;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容