C++如何实现回调函数?(函数指针与std::function)

C++回调函数核心是传递可调用逻辑,常用函数指针(轻量无状态)和std::function(灵活支持闭包与成员函数);函数指针适用于固定签名、无捕获的C风格API或性能敏感场景。

在C++中实现回调函数,核心是把“一段可调用的逻辑”作为参数传给另一个函数,让后者在合适时机执行它。常用方式有两种:传统函数指针(轻量、无状态)和 std::function(灵活、支持闭包与成员函数)。

用函数指针实现简单回调

函数指针适合固定签名、无捕获需求的场景,比如 C 风格 API 的兼容或性能敏感路径。

定义方式是声明一个指向特定参数/返回类型的函数的指针类型:

void on_click(int x, int y) {
    std::cout << "Clicked at (" << x << ", " << y << ")\n";
}

// 类型别名,提高可读性 using ClickHandler = void(*)(int, int);

void register_click_handler(ClickHandler handler) { // 保存或立即调用 if (handler) handler(100, 200); }

调用时直接传函数名(自动退化为指针):

register_click_handler(on_click);

⚠️ 注意:函数指针不能绑定局部变量、lambda(除非无捕获且显式转函数指针)、成员函数。

用 std::function 实现通用回调

std::function 是类型擦除容器,能容纳任意可调用对象(普通函数、lambda、bind 表达式、成员函数指针等),只要签名匹配。

使用前需包含头文件:#include

典型用法:

#include 
#include 

void run_callback(std::function cb) { if (cb) cb(42); }

int main() { // 普通函数 run_callback([](int x) { std::cout << "Lambda: " << x << "\n"; });

// 带捕获的 lambda(函数指针做不到)
int offset = 10;
run_callback([offset](int x) { std::cout << "With offset: " << x + offset << "\n"; });

// 绑定成员函数
struct Button {
    void click(int code) { std::cout << "Button code: " << code << "\n"; }
};
Button btn;
run_callback(std::bind(&Button::click, &btn, std::placeholders::_1));

}

处理成员函数回调

类成员函数有隐式 this 参数,不能直接当函数指针用。常见解法有三种:

  • 用 std::function + lambda 捕获 this:最直观,推荐用于类内回调注册
  • 用 std::bind 绑定对象和成员函数:明确表达绑定关系,但语法稍冗长
  • 静态成员函数 + void* 用户数据:C 风格兼容方案,需手动管理生命周期,易出错

例如,在类中注册自身方法为回调:

class EventManager {
public:
    void set_handler(std::function h) { handler_ = std::move(h); }
    void trigger() { if (handler_) handler(); }

private: std::function handler_; };

struct MyWidget { void handle_event() { std::cout << "Handled!\n"; }

void setup() {
    EventManager mgr;
    // 捕获 this,安全调用成员函数
    mgr.set_handler([this]() { this->handle_event(); });
    mgr.trigger();
}

};

选择建议与注意事项

  • 只调用全局/静态函数,且追求零开销 → 选函数指针
  • 需要 lambda、成员函数、状态捕获或统一接口 → 必选 std::function
  • std::function 有轻微运行时开销(类型擦除、可能堆分配),但现代编译器对简单 lambda 常做优化
  • 避免存储临时 lambda 的 std::function 并长期持有——若 lambda 捕获了局部变量,对象销毁后调用会崩溃
  • 考虑用模板参数替代 std::function 实现编译期多态(如接受任意 Callable),进一步避免运行时成本