如何修复 GCC 错误 'the lambda has no capture-default'
当遇到类似这样的 GCC 错误时:
gcc_lambda_error.txt
error: the lambda has no capture-default修复它通常很容易。查找捕获某个变量的 Lambda 函数,如下所示
lambda_capture_example.cpp
[&myVar] (/* ... */) {/* ... */}&myVar 表示*“通过引用捕获 myVar”*。
在大多数情况下,你可以使用捕获默认值来捕获所有局部变量:
lambda_capture_all_example.cpp
[&] (/* ... */) {/* ... */}在极少数情况下,这会产生意外的副作用,因为你现在通过引用捕获所有变量,而你可能想通过副本捕获某些变量 - 所以一定要检查你的代码。
注意此错误取决于 GCC 版本。对我来说使用 GCC 7.2 修复了错误。
Check out similar posts by category:
C/C++, GCC Errors
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow