[c++笔记] 仿函数

笔记自用 【C++11】深入剖析lambda仿函数的原理,自己动手实现std::function容器

#include <iostream>
#include <functional>

int main()
{
    int x = 2;
    std::function<void()> f = [x]() {
        printf("Hello %d!\n", x);
    };
    f();
    auto g = f;
    g();
    return 0;
}

接下来我们将一步一步实现仿函数

函数指针有什么应用场景?函数指针可以用于设计模式中的策略模式

void func_hello() {
    printf("Hello!\n");
}
void func_world() {
    printf("World!\n");
}

typedef void (*pfunc_t)();

void repeat_twice(pfunc_t func) {
    func();
    func();
}

int main()
{
    repeat_twice(func_hello);
    repeat_twice(func_world);
    return 0;
}

这段代码将会打印两遍Hello,两编World
代码中的 typedef void (*pfunc_t)(); 的含义是 void (*)() 描述了一个函数指针的类型,它指向一个没有参数并返回 void 的函数,pfunc_t 则是这种类型的新名称,不使用typedef的写法如下:

void repeat_twice(void (*func)())
{
    func();
    func();
}

新的问题:如果函数的参数列表不一致怎么办?
新的处理:给函数添加参数列表 void* arg

void func_hello(void* arg)
{
    printf("Hello!\n");
}
void func_world(void* arg)
{
    printf("World!\n");
}
void func_printnum(void* arg)
{
    int x = *(int*)arg; // 将void*强制转换为int*
    printf("%d\n", x);
}

typedef void (*pfunc_t)(void* arg);

void repeat_twice(pfunc_t func, void* arg)
{
    func(arg);
    func(arg);
}

int main()
{
    int x;
    std::cin >> x;
    repeat_twice(func_hello, nullptr);
    repeat_twice(func_world, nullptr);
    repeat_twice(func_printnum, &x);
    return 0;
}

新的问题:如果存在多个参数怎么办,比如一个x 一个 y
新的处理:我们可以定义一个结构体

struct prinnum_args_t {
    int x;
    int y;
};

void func_printnum(void *arg)
{
    auto a = (prinnum_args_t *)arg;
    printf("Numbers are %d, %d\n", a->x, a->y);
}

typedef void (*pfunc_t)(void *arg);

void repeat_twice(pfunc_t func, void *arg)
{
    func(arg);
    func(arg);
}

int main()
{
    int x, y;
    std::cin >> x >> y;
    prinnum_args_t a;
    a.x = x;
    a.y = y;
    repeat_twice(func_printnum, &a);
    return 0;
}

新的问题:上面这段代码实现了对x,y变量的捕获,但是当x y的值改变的时候我们还需要再次填充prinnum_args_t a,如何让函数打印的值能够跟随 x y 本身变换呢?

    int x, y;
    std::cin >> x >> y;
    repeat_twice(func_hello, nullptr);
    repeat_twice(func_world, nullptr);
    prinnum_args_t a;
    a.x = x;
    a.y = y;
    repeat_twice(func_printnum, &a);

    std::cin >> x >> y; // x y 发生改变
    a.x = x; // 需要再次填充prinnum_args_t 
    a.y = y;
    repeat_twice(func_printnum, &a);

新的处理:需要使用指针或者引用来实现变量的同步变化

struct prinnum_args_t {
    int &x;
    int &y;
};

void func_printnum(void *arg)
{
    auto a = (prinnum_args_t *)arg;
    printf("Numbers are %d, %d\n", a->x, a->y);
}

typedef void (*pfunc_t)(void *arg);

void repeat_twice(pfunc_t func, void *arg)
{
    func(arg);
    func(arg);
}

int main()
{
    int x, y;
    std::cin >> x >> y;
    prinnum_args_t a{x, y};
    repeat_twice(func_printnum, &a);

    std::cin >> x >> y;
    repeat_twice(func_printnum, &a); // 将会打印新的 x y 值
    return 0;
}

可以直接将这个函数作为结构体的成员函数,从而实现这个结构体就是函数本身,这就是仿函数的起源

struct prinnum_args_t {
    void call()
    {
        printf("Numbers are %d, %d\n", x, y);
    }
    int &x;
    int &y;
};

template <class Fn>
void repeat_twice(Fn func)
{
    func.call();
    func.call();
}

int main()
{
    int x, y;
    std::cin >> x >> y;
    prinnum_args_t a{x, y};
    repeat_twice(a);

    std::cin >> x >> y;
    repeat_twice(a);
    return 0;
}

事实上,现在我们可以直接重载operator(),也即在类内部定义operator()函数,它将作为类的调用操作

struct prinnum_args_t {
    void operator()()
    {
        printf("Numbers are %d, %d\n", x, y);
    }
    int &x;
    int &y;
};

template <class Fn>
void repeat_twice(Fn func)
{
    func();
    func();
}

int main()
{
    int x, y;
    std::cin >> x >> y;
    prinnum_args_t a{x, y};
    repeat_twice(a);

    std::cin >> x >> y;
    repeat_twice(a);
    return 0;
}

再加上 const,便于处理 const 参数

struct prinnum_args_t {
    void operator()() const
    {
        printf("Numbers are %d, %d\n", x, y);
    }
    int &x;
    int &y;
};

template <class Fn>
void repeat_twice(Fn const &func)
{
    func();
    func();
}

然后我们可以对比一下c++的仿函数,本质上和我们上面实现的这个类是一样的,只是使用了一个语法糖
c++ 仿函数本质上就是使用了一个语法糖构造了一个类,然后调用了这个类的operator()方法

int main()
{
    int x, y;
    std::cin >> x >> y;
    prinnum_args_t a{x, y};
    // c++ 仿函数本质上就是使用了一个语法糖构造了一个类,然后调用了这个类的operator()方法
    repeat_twice([&x, &y]() { printf("Numbers are %d, %d\n", x, y); });

    std::cin >> x >> y;
    repeat_twice(a);
    return 0;
}

也即 repeat_twice([&x, &y]() { printf("Numbers are %d, %d\n", x, y); }); 等价于


c++内置的这个仿函数调用的也是一个const函数,可以看到x通过引用捕获,y通过值捕获时,可以修改x,但不能修改y
在这里插入图片描述
为了便于理解,这个仿函数等价于下面这个类
在这里插入图片描述
如果想修改,需要加上mutable来去除const

新的问题:仿函数怎么传入参数?
新的处理:operator()可以直接接收参数

struct func_prinnum_t {
    void operator()(int i) const // 传入参数 int i
    {
        printf("#%d Numbers are %d, %d\n", i, x, y);
    }
    int x;
    int y;
};

template <class Fn>
void repeat_twice(Fn const &func)
{
    func(1);
    func(2);
}

int main()
{
    int x, y;
    std::cin >> x >> y;
    repeat_twice([=](int i) { // 传入参数 int i
        printf("#%d Numbers are %d, %d\n", i, x, y);
    });
    printf("----------------\n");
    func_prinnum_t func_prinnum{x, y};
    repeat_twice(func_prinnum);
    return 0;
}

新的问题:由于使用了模板,所以无法实现声明和定义的分离,得写在一个文件里面,很不方便
待解读 Function.hpp

  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值