当我将可变 lambda 作为 const 引用传递时,代码实际上会做什么?
为什么编译器不引发错误,这是一个未定义的操作吗?
std::function<void()>为什么f1和f2使用的f1和f2使用的不同auto?
我发现了一个类似的问题,但我仍然不完全明白
const std::function 包装非常量运算符() / 可变 lambda
#include <iostream>
#include <functional>
void call(std::function<void()> const & cb) {
cb();
cb();
}
int main() {
std::function<void()> f1 = [a = 0] () mutable {
std::cout << ++a << std::endl;
};
call(f1); // prints 1 2
call(f1); // prints 3 4
auto f2 = [a = 0] () mutable {
std::cout << ++a << std::endl;
};
call(f2); // prints 1 2
call(f2); // …Run Code Online (Sandbox Code Playgroud)