C++ 使用lambda表达式作为函数参数

C++ 使用lambda表达式作为函数参数

1. 使用模板参数实现

#include <iostream>
#include <string>

#include <unistd.h>

volatile bool g_is_running = true;
volatile bool g_is_idle = false;

template <typename T, typename F>
int32_t EventLoop(const T& do_func, const F& do_idle) {
    while (g_is_running)
    {
        if (!g_is_idle)
        {
            int32_t ec = do_func();
            if (ec != 0)
            {
                break;
            }
        }
        else
        {
            do_idle();
        }
    }
    g_is_running = false;
    return 0;
}

int main(int argc, char ** argv) {
    std::cout << "Hello, World!" << std::endl;

    std::string language = "Rust";
    auto do_biz = [&]() { 
        std::cout << "Welcome to " << language << std::endl;
        sleep(1);
        return 0;
    };

    auto do_idle = []() { 
        usleep(1);
    };

    return EventLoop(do_biz, do_idle);
}

2. 使用std::function方式实现

#include <iostream>
#include <string>

#include <unistd.h>
#include <functional>

volatile bool g_is_running = true;
volatile bool g_is_idle = false;


int32_t EventLoop(const std::function<int(void)>& do_func, const std::function<int(void)>& do_idle) {
    while (g_is_running)
    {
        if (!g_is_idle)
        {
            int32_t ec = do_func();
            if (ec != 0)
            {
                break;
            }
        }
        else
        {
            do_idle();
        }
    }
    g_is_running = false;
    return 0;
}

int main(int argc, char ** argv) {
    std::cout << "Hello, World!" << std::endl;

    std::string language = "Rust";
    auto do_biz = [&]() { 
        std::cout << "Welcome to " << language << std::endl;
        sleep(1);
        return 0;
    };

    auto do_idle = []() { 
        usleep(1);
        return 0;
    };

    return EventLoop(do_biz, do_idle);
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
Lambda表达式C++11引入的一种函数对象,可以在需要函数对象的地方使用,比如作为函数参数、返回值等。Lambda表达式的语法形式如下: ``` [capture list](parameters) mutable exception -> return type { // function body } ``` 其中,`capture list` 表示捕获列表,用于捕获外部变量。`parameters` 表示函数参数列表,`mutable` 用于表示是否可以修改值传递的变量,`exception` 是异常列表,`return type` 表示返回类型,`function body` 则是函数体。 在Lambda表达式中,可以通过 `[this]` 捕获当前对象的指针,即 `this` 指针,可以方便地访问当前对象的成员变量和成员函数。例如: ``` class MyClass { public: void foo() { int x = 1; auto lambda = [this, x]() mutable { this->m_member_var += x; this->m_member_function(); x++; }; lambda(); } private: int m_member_var; void m_member_function(); }; ``` 在上面的例子中,Lambda表达式通过 `[this, x]` 捕获了当前对象的指针和 `foo()` 函数中定义的变量 `x`。在 Lambda 表达式中可以通过 `this->m_member_var` 和 `this->m_member_function()` 访问当前对象的成员变量和成员函数。由于 `x` 是值传递的,所以在 Lambda 表达式中需要使用 `mutable` 关键字使其可修改,可以通过 `x++` 修改变量的值。最后调用 `lambda()` 执行 Lambda 表达式。 需要注意的是,Lambda表达式捕获 `this` 指针时,需要保证当前对象是有效的,即不能在已经销毁的对象中访问成员变量和成员函数。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Erice_s

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值