小编Shu*_*Guo的帖子

如果我将可变 lambda 作为 const 引用传递给函数,会发生什么?

当我将可变 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)

c++ lambda c++11 c++14 c++17

3
推荐指数
1
解决办法
267
查看次数

标签 统计

c++ ×1

c++11 ×1

c++14 ×1

c++17 ×1

lambda ×1