代码:
#include <iostream>
template <class FunctorType>
void caller(const FunctorType& func) {
func();
}
int main() {
double data[5] = {5., 0., 0., 0., 0.};
auto peek_data = [data]() { std::cout << data[0] << std::endl; };
auto change_data = [data]() mutable { data[0] = 4.2; };
caller(peek_data); // This works
caller(change_data); // This doesn't
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如果我用 编译它clang++ -std=c++11 mutable_lambda.cpp,我得到了
error: no matching function for call to object of type 'const (lambda at mutable_lambda.cpp:8:22)'。
问题:为什么通过可变副本捕获传递第二个 lambda …