DEV Community

Cover image for Registering callback using std::function in C++
pikoTutorial
pikoTutorial

Posted on • Originally published at pikotutorial.com

Registering callback using std::function in C++

Welcome to the next pikoTutorial !

Imagine you're building an application that processes some data and once it's done, the user needs to be notified about the result. Since you don’t want to hard-code the notification logic, it’s better to allow the user to register a custom function that will be called automatically after the data processing completes.

Using std::function for callback registration

One of the most flexible ways to implement callbacks in C++ is by using std::function. std::function can store and invoke any callable object (functions, lambdas, or function objects) that matches a specific signature. The overall syntax is:

std::function<function_return_type(function_argument_types)> name;
Enter fullscreen mode Exit fullscreen mode

Let’s define a ProcessData unction that accepts a callback and notifies the user once processing is complete.

#include <iostream>
#include <functional>
// Type alias for the callback type
using CallbackType = std::function<void(const bool)>;
// Function accepting callback as an argument
void ProcessData(const CallbackType& callback) {
    // Simulate data processing
    std::cout << "Processing data" << std::endl;
    // Notify user via callback
    callback(true);
}
// Callback function definition
void Callback(const bool result) {
    std::cout << "Callback called! Result: " << result << std::endl;
}

int main() {
    // Register a callback using an existing function
    ProcessData(Callback);
    // Register a callback using lambda function
    ProcessData([](const bool result) {
        std::cout << "Lambda called! Result: " << result << std::endl;
    });
    // Register using callable object
    CallbackType cb = [](const bool result){
        std::cout << "Callable called! Result: " << result << std::endl;
    };
    ProcessData(cb);
}
Enter fullscreen mode Exit fullscreen mode

Postmark Image

20% off for developers who'd rather build features than debug email

Stop wrestling with email delivery and get back to the code you love. Postmark handles the complexities of email infrastructure so you can ship your product faster.

Start free

Top comments (0)

ACI image

ACI.dev: Fully Open-source AI Agent Tool-Use Infra (Composio Alternative)

100% open-source tool-use platform (backend, dev portal, integration library, SDK/MCP) that connects your AI agents to 600+ tools with multi-tenant auth, granular permissions, and access through direct function calling or a unified MCP server.

Check out our GitHub!

Join the Runner H "AI Agent Prompting" Challenge: $10,000 in Prizes for 20 Winners!

Runner H is the AI agent you can delegate all your boring and repetitive tasks to - an autonomous agent that can use any tools you give it and complete full tasks from a single prompt.

Check out the challenge

DEV is bringing live events to the community. Dismiss if you're not interested. ❤️