Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте Returning Lambdas | Advanced Functional Patterns
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
C++ Functional Utilities

bookReturning Lambdas

When you return a lambda from a function, C++ can often deduce the return type automatically using auto. This is convenient and keeps your code concise. However, there are cases where you might want to specify an explicit return type, such as std::function, for greater flexibility.

Using std::function as the return type allows you to return any callable object matching the signature, not just a specific lambda. This can be helpful if you want to change the implementation later, or if you need to store different types of callables in a container or pass them around in your code. However, using std::function may introduce some overhead compared to using auto-deduced lambdas directly, so you should choose the approach that best fits your needs.

main.cpp

main.cpp

copy
12345678910111213141516171819202122
#include <iostream> #include <functional> // Function returning a lambda that multiplies by a captured factor auto make_multiplier(int factor) { // The lambda captures 'factor' by value return [factor](int x) { return x * factor; }; } int main() { // Create a multiplier that multiplies by 5 auto times_five = make_multiplier(5); std::cout << "3 * 5 = " << times_five(3) << std::endl; // Output: 15 // Using std::function as the return type std::function<int(int)> times_ten = make_multiplier(10); std::cout << "4 * 10 = " << times_ten(4) << std::endl; // Output: 40 return 0; }
question mark

Why might you use std::function as a return type when returning a lambda from a function?

Select the correct answer

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

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

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

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

Запитати АІ

expand

Запитати АІ

ChatGPT

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

bookReturning Lambdas

Свайпніть щоб показати меню

When you return a lambda from a function, C++ can often deduce the return type automatically using auto. This is convenient and keeps your code concise. However, there are cases where you might want to specify an explicit return type, such as std::function, for greater flexibility.

Using std::function as the return type allows you to return any callable object matching the signature, not just a specific lambda. This can be helpful if you want to change the implementation later, or if you need to store different types of callables in a container or pass them around in your code. However, using std::function may introduce some overhead compared to using auto-deduced lambdas directly, so you should choose the approach that best fits your needs.

main.cpp

main.cpp

copy
12345678910111213141516171819202122
#include <iostream> #include <functional> // Function returning a lambda that multiplies by a captured factor auto make_multiplier(int factor) { // The lambda captures 'factor' by value return [factor](int x) { return x * factor; }; } int main() { // Create a multiplier that multiplies by 5 auto times_five = make_multiplier(5); std::cout << "3 * 5 = " << times_five(3) << std::endl; // Output: 15 // Using std::function as the return type std::function<int(int)> times_ten = make_multiplier(10); std::cout << "4 * 10 = " << times_ten(4) << std::endl; // Output: 40 return 0; }
question mark

Why might you use std::function as a return type when returning a lambda from a function?

Select the correct answer

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

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

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

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