调用时为 std::function 分配新值
Assign new value to std::function while calling
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
。
这种做法能保证安全吗?
lambda 的代码部分被编译为机器代码,并且在赋值期间仅分配指向该代码的指向函数的指针。因此,只要您不再使用捕获的变量,重新分配保存 lambda 的变量应该是安全的,因为不会更改任何运行代码。
相关文章:
- C++使用另一个数组和新值初始化数组
- 变体 - 分配新值时是否清理旧值?
- C++:我可以在线程仍在运行时为线程提供新值,还是必须先结束它?
- C++类析构函数使用新值而不是实际值
- 写入新值是构成前增量表达式"value computation"的一部分,还是"side effect"?
- 我们是否可以保证任何原子写入都会立即将原子变量的新值存储在主存储器中?
- C++ - 读取进程内存到缓冲区,写入进程内存(同一缓冲区上的新值)将缓冲区恢复为旧值
- 将新值添加到链表中
- C 是否有办法使程序运行,只要引入了新值
- 如何在while循环之外分配一个新值
- 将const char * const参数成员分配给新值
- 常量字符* 向量的新值没有得到 push_back()
- C++ 变量未正确从向量接收新值
- 将变量传递给函数返回包含该变量的新值重复直到值达到 0
- 如何在类构造函数中创建新值并分配给私有unique_ptr
- 在C++或Python中,将浮点值映射到指定范围内的新值
- 调用时为 std::function 分配新值
- 在为其分配一个新值之前,应将`nullptr`分配给`sTD :: simelod_ptr`
- 为什么函数没有在每个循环中提供新值
- 如果我尚未为指针分配新值,是否需要删除指针?