Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте 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

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 3. Розділ 2

Запитати АІ

expand

Запитати АІ

ChatGPT

Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат

Suggested prompts:

Can you give an example of how to use std::function with a lambda?

What are some common use cases for std::function in real-world applications?

How does std::function compare to using function pointers or templates?

Awesome!

Completion rate improved to 14.29

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

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 3. Розділ 2
some-alt