Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprende Callable Objects and std::function | Functional Programming
Quizzes & Challenges
Quizzes
Challenges
/
C++ Modern Features

bookCallable Objects and std::function

The std::function template in modern C++ provides a flexible way to store, pass, and invoke any callable object with a compatible signature. A callable object in C++ is anything that can be called using the function call syntax, such as regular functions, lambda expressions, functors (objects with operator()), and even function pointers. The syntax for declaring a std::function is std::function<ReturnType(ArgTypes...)>, where you specify the desired return type and argument types. This abstraction allows you to write code that can accept, store, or return different kinds of callables, making your programs more modular and dynamic. By using std::function, you can change the behavior of code at runtime by assigning different callables to the same variable.

main.cpp

main.cpp

copy
1234567891011121314151617181920
#include <iostream> #include <functional> int square(int x) { return x * x; } int main() { // Store a regular function std::function<int(int)> func = square; std::cout << "square(5): " << func(5) << std::endl; // Store a lambda expression func = [](int x) { return x + 10; }; std::cout << "lambda(5): " << func(5) << std::endl; // Store another lambda with capture int offset = 3; func = [offset](int x) { return x * 2 + offset; }; std::cout << "lambda with capture(5): " << func(5) << std::endl; }

When using std::function, it is important to be aware of its performance characteristics. std::function provides great flexibility, but this comes at the cost of some overhead compared to directly invoking a function pointer or a lambda. This overhead is generally small, but can be significant in performance-critical code, such as tight loops or low-latency applications. If you do not need the ability to store different types of callables in a single variable, prefer direct function pointers or template parameters for maximum efficiency. Use std::function when you require runtime polymorphism for callables, such as in callback interfaces or event systems.

question mark

Which statement best describes the usage and characteristics of std::function in modern C++?

Select the correct answer

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 3. Capítulo 2

Pregunte a AI

expand

Pregunte a AI

ChatGPT

Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla

Awesome!

Completion rate improved to 14.29

bookCallable Objects and std::function

Desliza para mostrar el menú

The std::function template in modern C++ provides a flexible way to store, pass, and invoke any callable object with a compatible signature. A callable object in C++ is anything that can be called using the function call syntax, such as regular functions, lambda expressions, functors (objects with operator()), and even function pointers. The syntax for declaring a std::function is std::function<ReturnType(ArgTypes...)>, where you specify the desired return type and argument types. This abstraction allows you to write code that can accept, store, or return different kinds of callables, making your programs more modular and dynamic. By using std::function, you can change the behavior of code at runtime by assigning different callables to the same variable.

main.cpp

main.cpp

copy
1234567891011121314151617181920
#include <iostream> #include <functional> int square(int x) { return x * x; } int main() { // Store a regular function std::function<int(int)> func = square; std::cout << "square(5): " << func(5) << std::endl; // Store a lambda expression func = [](int x) { return x + 10; }; std::cout << "lambda(5): " << func(5) << std::endl; // Store another lambda with capture int offset = 3; func = [offset](int x) { return x * 2 + offset; }; std::cout << "lambda with capture(5): " << func(5) << std::endl; }

When using std::function, it is important to be aware of its performance characteristics. std::function provides great flexibility, but this comes at the cost of some overhead compared to directly invoking a function pointer or a lambda. This overhead is generally small, but can be significant in performance-critical code, such as tight loops or low-latency applications. If you do not need the ability to store different types of callables in a single variable, prefer direct function pointers or template parameters for maximum efficiency. Use std::function when you require runtime polymorphism for callables, such as in callback interfaces or event systems.

question mark

Which statement best describes the usage and characteristics of std::function in modern C++?

Select the correct answer

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 3. Capítulo 2
some-alt