C++

C++ - Functions, Lambda

Posted by Rico's Nerd Cluster on February 13, 2023

Lambda Basics

Lambda functions are introduced when?

Generic lambda (cpp14+) can take in an arbitrary type

One example of lambda is when working with lots of memcpy ``` You can use a generic lambda (available since C++14) to make copyToBuffer work with any type:

1
2
3
4
5
6
size_t pos = 0;
auto copyToBuffer = [&](const auto& variable) {
    memcpy(buffer + pos, &variable, sizeof(variable));
    pos += sizeof(variable);
}
```