Skip to content

Commit

Permalink
Convert capture to parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
JayBazuzi committed Sep 17, 2018
1 parent 31e4641 commit 8f6c1e5
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions recipes/micro-step-helpers/convert-capture-to-parameter/cpp.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{{< Staging >}}

# Recipe

## Given:

A lambda which captures by reference:

```cpp
std::string s = ...

return [&]() {
std::cout << s;
}();
```

Add the variable to the parameter list as `auto&` and pass it as an argument, using the same name in both places.

```cpp
std::string s = ...

return [&](auto& s) {
std::cout << s;
}(s);
```

Consider marking the parameter as `const` as a future refactoring step.

0 comments on commit 8f6c1e5

Please sign in to comment.