Let's say I have two local smart pointers, foo
and bar
.
shared_ptr<Foo> foo = ...
shared_ptr<Bar> bar = ...
These smart pointers are wrappers around resources that for some reason must be destructed in the order foo
, then bar
.
Now I want to create a lambda that uses foo
and bar
, but outlives the scope containing them. So I'd capture them by value, like this:
auto lambda = [foo, bar]() { ... };
This creates copies of foo
and bar
within the function object. When the function object is destructed, these copies will be destructed, as well, but I care about the order in which this happens. So my question is:
When a lambda object is destructed, in what order are its by-value captures destructed? And how can I (hopefully) influence this order?
[=]
. – Darla[foo,bar]
is equivalent to[=foo,=bar]
i.e. it is a copy. – Perhaps[=]
, i.e. consider what the declaration order would be without listing the variables one's self. (Obviously it's a moot point now since the declaration order is unspecified regardless of how ones does the captures.) – Gate