-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
27 additions
and
0 deletions.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
recipes/micro-step-helpers/convert-capture-to-parameter/cpp.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |