Skip to content

Commit

Permalink
feat!: Add SBO support for lambdas and functors delegates
Browse files Browse the repository at this point in the history
This commit adds 'Small Buffer Optimization' to functor and lambda functions with
small footprint, i.e. the same size as 'void *' or less, and will therefore mimic the
`std::function` behavior for these types, instead of being a reference
to them as before.

This means that the following operations are now allowed to delegate
with deferred/out-of-scope calls:

```
etl::delegate<void()> Class::method() {
  etl::delegate<void()> d {[this](){ this->do_something(); }};
  return d;
}
```

A caveat is that the old behavior where everything became a reference instead
of an object is changed and the user must now use a `reference_wrapper`
to gain the same behavior, i.e. using `etl::ref` or `etl::cref`:

```
  auto f = [&](){ /* ... */ };
  etl::delegate<void()> d { etl::ref(d) };
  d();
```

This change of behavior also matches the C++ Core Guidelines for how
to pass parameters
[https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#f15-prefer-simple-and-conventional-ways-of-passing-information]

BREAKING CHANGE: functors and lambda capture will not be references to any
longer.
  • Loading branch information
devjoa committed Mar 23, 2024
1 parent 82ecb70 commit 746a4fa
Show file tree
Hide file tree
Showing 3 changed files with 426 additions and 40 deletions.
Loading

0 comments on commit 746a4fa

Please sign in to comment.