【发布时间】:2016-12-02 17:46:38
【问题描述】:
int i = 9;
struct_variable.f = [i](T struct_variable&) {
do_something_with_capture_variable(i);
...
struct_variable.f = another_compatible_std_function;
//do something else, but never use captured variable after here
...
};
struct_variable.f(struct_variable);
lambda 函数保存为成员struct_variable.f(也被输入std::function),在回调中,使用完捕获的变量后,struct_variable.f 被another_compatible_std_function 替换。
这种做法能保证安全吗?
【问题讨论】:
-
我想说是的,只要你小心点,因为这种情况类似于
delete-在执行其成员函数之一期间对对象执行操作。但这很难从标准中证明。