Detailed explanation and implementation of c + + callback function (lambda)

A callback function is a function that is passed as a parameter. In C language, callback function can only be implemented by function pointer. In C + + language, you can also use imitative function or anonymous function. The use of callback function can greatly improve the efficiency of programming, which makes it widely used in modern programming.

Benefits of callback

It is used for decoupling and can separate the caller from the callee, so the caller does not care who is the callee. It only needs to know that there is a called function with specific prototypes and constraints. In short, the callback function allows the user to pass the pointer of the function to be called as a parameter to a function, so that the function can flexibly use different methods when handling similar events.

Callbacks can also be used for notification mechanisms. When an event occurs, if the user has registered a callback function, the contents of the callback function will be automatically executed.

Callbacks can also be used to reserve logic. For example, if you are uncertain or not sure what operation to perform under a certain condition, you can leave the choice to the user to implement the corresponding logic. For example, for some function libraries, the implementation of sorting algorithms, in order to make the library more general, does not want to embed sorting logic in the functions, but allows users to implement the corresponding logic.

For example, between the application developer and the kernel, the application registers the signal processing, which is actually a callback registration.

As shown in the following figure:


Use of callbacks

⑴ define a callback function.

⑵ the party providing the function implementation registers the function pointer of the callback function to the caller during initialization.

(3) when a specific event or condition occurs, the caller causes the permanent function pointer to call the callback function to process the event.

Implementation of c + + callback

Most of the examples on the Internet are too old. They do not use the features of modern c + +, or the implementation of function pointers in the past.

The following example is the implementation above c++14, including the use of lambda expressions.

#include <functional>
#include <iostream>

class MyTest{
    MyTest() = default;
    void doCalc(){
        //Do something else. It's over
        // Execute callback
        if(myCallBack!= nullptr){

    using callback_t = std::function<void(const int &a, const int &b)>;

    // Registering Callbacks 
    void setCallBackHandler(const callback_t &cb){
        myCallBack = cb;

    // Define callback
    callback_t myCallBack;

// Callback function
void handleCallBack(const int &a,const int &b){
    std::cout << "this is from callback handleCallBack"<<std::endl;

int main(){

    MyTest t;

    // Callback function
    auto f= [](const int &a,const int &b){
        std::cout << "this is from callback f"<<std::endl;
    // Registering Callbacks 
    // Writing method I

    // Writing method II
    t.setCallBackHandler([&f](auto &&a, auto &&b) {
        f(std::forward<decltype(a)>(a), std::forward<decltype(b)>(b));

    // Writing method III
    t.setCallBackHandler([](auto &&a, auto &&b) {
        handleCallBack(std::forward<decltype(a)>(a), std::forward<decltype(b)>(b));


The above formal parameters are written in a way similar to auto, which is a feature of c++14.

As for the parameters, why do you use & &? In order to achieve perfect forwarding. To do this, the parameter must become a generic reference (see Clause 24).

Clause 33: use decltype to std::forward for auto & & formal parameters.
One of the most exciting features of C++14 is the use of auto's generic lambda in the parameter specification.  

In lambda, we can determine whether the actual parameter is an lvalue or an rvalue by checking the type of the parameter x.

Clause 28 explains that if an lvalue argument is passed to a general reference, the type of the parameter will become an lvalue reference. If an rvalue is passed, the parameter will become an rvalue reference. This means that in lambda, we can determine whether the argument is an lvalue or an rvalue by checking the type of the parameter x.

Decltype (see clause 3) gives us an implementation path. If it is an lvalue, the type obtained by decltype(x) is an lvalue reference; if it is an rvalue, the type obtained by decltype(x) is an rvalue reference.

Remember: use decltype to std::forward for auto & & formal parameters.

Please refer to Effective.Modern.C + + for details of the above terms.

Implementation of c language

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef int (callBack)(const void *buffer,size_t size,char *p_out);

void callFunc(callBack *my_callback, char *p_out) {
    const void *buffer = NULL;
    my_callback(buffer,0,p_out); //You can fill in any value you want

int callBackFunc(const void *buffer, size_t size, char *p_out){
    strcpy(p_out,"this is Callback:this is string.");
    return 1;

int main(int argc,char *args[]){
    char p_out[100];
    return 0;


What is a callback function? Why use callback functions? How to use callback functions_ llzhang_fly's blog - CSDN blog_ Callback function

Basic understanding and use of C + + callback function_ Once a mortal blog - CSDN blog_ Callback function 

Essence of callback function - what is callback function and why to use callback function_ Douqu blog - CSDN blog_ Callback function

The principle of c++11 thread pool and the use of callback function_ Maverick cat a's blog - CSDN blog_ c + + multithread callback function In depth understanding: callback function_ Geek blog - CSDN blog_ Callback function

Callback function for C + + Learning_ Autodesk_ Glodon's blog - CSDN blog 

On C + + callback function (callback) concise and practical_ zhoupian's blog - CSDN blog_ c++ callback

Tags: C++ Lambda programming language

Posted by darkhappy on Mon, 22 Aug 2022 02:50:43 +0300