A pass at improving F.52. Closes #884

This commit is contained in:
hsutter 2017-04-17 11:42:08 -07:00
parent 6d01cfd99b
commit 323912e609

View File

@ -3535,6 +3535,25 @@ There is not a choice when a set of functions are used to do a semantically equi
For efficiency and correctness, you nearly always want to capture by reference when using the lambda locally. This includes when writing or calling parallel algorithms that are local because they join before returning.
##### Discussion
The efficiency consideration is that most types are cheaper to pass by reference than by value.
The correctness consideration is that many calls want to perform side effects on the original object at the call site (see example below). Passing by value prevents this.
##### Note
Unfortunately, there is no simple way to capture by reference to `const` to get the efficiency for a local call but also prevent side effects.
##### Example
Here, a large object (a network message) is passed to an iterative algorithm, and is it not efficient or correct to copy the message (which may not be copyable):
std::for_each(begin(sockets), end(sockets), [&message](auto& socket)
{
socket.send(message);
});
##### Example
This is a simple three-stage parallel pipeline. Each `stage` object encapsulates a worker thread and a queue, has a `process` function to enqueue work, and in its destructor automatically blocks waiting for the queue to empty before ending the thread.
@ -3549,7 +3568,7 @@ This is a simple three-stage parallel pipeline. Each `stage` object encapsulates
##### Enforcement
???
Flag a lambda that captures by reference, but is used other than locally within the function scope or passed to a function by reference. (Note: This rule is an approximation, but does flag passing by pointer as those are more likely to be stored by the callee, writing to a heap location accessed via a parameter, returning the lambda, etc. The Lifetime rules will also provide general rules that flag escaping pointers and references including via lambdas.)
### <a name="Rf-value-capture"></a>F.53: Avoid capturing by reference in lambdas that will be used nonlocally, including returned, stored on the heap, or passed to another thread