mirror of
https://github.com/isocpp/CppCoreGuidelines.git
synced 2024-03-22 13:30:58 +08:00
F.53: Minor fix.
This commit is contained in:
parent
4fc0beafc3
commit
fdd91e959a
|
@ -3152,18 +3152,20 @@ Pointers and references to locals shouldn't outlive their scope. Lambdas that ca
|
|||
##### Example, bad
|
||||
|
||||
{
|
||||
int local_variable = 42;
|
||||
background_thread.queue_work([&]{ process(local_variable); }); // Want a reference to local_variable.
|
||||
// Note, that after program exists this scope, local_variable does no longer exist,
|
||||
int local = 42;
|
||||
thread_pool.queue_work([&]{ process(local); }); // Want a reference to local.
|
||||
// Note, that after program exists this scope,
|
||||
// local does no longer exist,
|
||||
// therefore process() call will have undefined behavior!
|
||||
}
|
||||
|
||||
##### Example, good
|
||||
|
||||
{
|
||||
int local_variable = 42;
|
||||
background_thread.queue_work([=]{ process(local_variable); }); // Want a copy of local_variable.
|
||||
// Since a copy of local_variable is made, it will be available at all times for the call.
|
||||
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
|
||||
|
|
Loading…
Reference in New Issue
Block a user