C++20 Lambda Features šŸŽÆ

beginner
19 min

C++20 Lambda Features šŸŽÆ

Welcome back to CodeYourCraft! Today, we're diving into the exciting world of C++20 Lambda Features. If you're new to programming, don't worry! We'll cover everything from the basics to advanced examples. Let's get started!

What are Lambda Functions? šŸ“

Lambda functions, or lambdas, are anonymous functions that can be used to define function objects without needing a name. They were introduced in C++11 but have been enhanced in C++20.

Why are they useful? Lambdas make your code cleaner and more flexible, as they allow you to define and use functions inline, right where you need them.

Basic Lambda Syntax šŸ’”

A lambda function has the following structure:

cpp
[capture](parameters) -> return_type { function_body }
  1. [capture]: This part is optional and specifies what the lambda function captures from its enclosing scope.
  2. (parameters): The parameters passed to the lambda function, similar to a regular function.
  3. -> return_type: The type of value the lambda function returns. If not specified, the return type is auto.
  4. { function_body }: The code that gets executed when the lambda function is called.

Let's see a simple example:

cpp
#include <iostream> int main() { auto my_lambda = [](int x, int y) -> int { return x + y; }; std::cout << my_lambda(5, 3); // Output: 8 }

In this example, we created a lambda function that takes two integers and returns their sum. We then called the lambda function and printed the result.

C++20 Lambda Features šŸŽÆ

C++20 has introduced several improvements to lambda functions, making them even more powerful and versatile. Let's explore some of the key features:

Default Capture

If no capture clause is provided, the lambda function captures nothing by default. However, if you want the lambda to capture its enclosing scope's variables, you can use the default capture syntax:

cpp
[=]() { /* function body */ };

This lambda captures all variables from the enclosing scope by value.

Capture by Reference

To capture variables by reference, use the & symbol:

cpp
[&]() { /* function body */ };

Capture Specific Variables

You can also capture specific variables by name:

cpp
int x = 5; auto my_lambda = [=, &x]() { std::cout << x; }; my_lambda(); // Output: 5

Lambda with Return Type

If you want to specify the return type of a lambda, you can do so like this:

cpp
auto my_lambda = [](int x, int y) -> int { return x + y; };

Inline Namespace

By default, lambdas are placed in an anonymous namespace, but you can create an inline namespace for them:

cpp
namespace { auto my_lambda = [](int x, int y) -> int { return x + y; }; } int main() { std::cout << my_lambda(5, 3); // Output: 8 }

Lambda with Attributes

You can also add attributes to your lambda functions:

cpp
__attribute__((warn_unused_result)) auto my_lambda(int x, int y) -> int { return x + y; }

Practical Usage šŸ’”

Lambdas can be used in many real-world scenarios, such as sorting, filtering, and mapping collections, or defining simple callback functions.

Here's an example of using a lambda to sort a vector:

cpp
#include <iostream> #include <vector> #include <algorithm> int main() { std::vector<int> numbers = {5, 3, 1, 4, 2}; std::sort(numbers.begin(), numbers.end(), [](int a, int b) { return a > b; }); for (const auto &number : numbers) { std::cout << number << " "; // Output: 5 4 3 2 1 } }

In this example, we sorted the numbers vector in descending order using a lambda function.

Quiz Time! šŸŽÆ

Quick Quiz
Question 1 of 1

What does the `[=]()` capture clause do?

We hope you enjoyed learning about C++20 Lambda Features! Stay tuned for more exciting lessons here at CodeYourCraft. Happy coding! šŸ’»šŸ’»šŸ’»