mirror of
https://github.com/isocpp/CppCoreGuidelines.git
synced 2024-03-22 13:30:58 +08:00
Merge pull request #520 from PiotrSliwa/F.53
More comprehensive example and enforcements for F.53
This commit is contained in:
commit
3fe4e40ae9
|
@ -3163,18 +3163,29 @@ This is a simple three-stage parallel pipeline. Each `stage` object encapsulates
|
||||||
|
|
||||||
Pointers and references to locals shouldn't outlive their scope. Lambdas that capture by reference are just another place to store a reference to a local object, and shouldn't do so if they (or a copy) outlive the scope.
|
Pointers and references to locals shouldn't outlive their scope. Lambdas that capture by reference are just another place to store a reference to a local object, and shouldn't do so if they (or a copy) outlive the scope.
|
||||||
|
|
||||||
##### Example
|
##### Example, bad
|
||||||
|
|
||||||
{
|
{
|
||||||
// ...
|
int local = 42;
|
||||||
|
thread_pool.queue_work([&]{ process(local); }); // Want a reference to local.
|
||||||
|
// Note, that after program exits this scope,
|
||||||
|
// local no longer exists, therefore
|
||||||
|
// process() call will have undefined behavior!
|
||||||
|
}
|
||||||
|
|
||||||
// a, b, c are local variables
|
##### Example, good
|
||||||
background_thread.queue_work([=]{ process(a, b, c); }); // want copies of a, b, and c
|
|
||||||
|
{
|
||||||
|
int local = 42;
|
||||||
|
thread_pool.queue_work([=]{ process(local); }); // Want a copy of local.
|
||||||
|
// Since a copy of local is made, it will be
|
||||||
|
// available at all times for the call.
|
||||||
}
|
}
|
||||||
|
|
||||||
##### Enforcement
|
##### Enforcement
|
||||||
|
|
||||||
???
|
* (Simple) Warn when capture-list contains a reference to a locally declared variable
|
||||||
|
* (Complex) Flag when capture-list contains a reference to a locally declared variable and the lambda is passed to a non-`const` and non-local context
|
||||||
|
|
||||||
### <a name="Rf-this-capture"></a>F.54: If you capture `this`, capture all variables explicitly (no default capture)
|
### <a name="Rf-this-capture"></a>F.54: If you capture `this`, capture all variables explicitly (no default capture)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user