How to fix GCC error 'the lambda has no capture-default'

When encountering a GCC error like this:

example.txt
error: the lambda has no capture-default

fixing it is usually quite easy. Look for a Lambda function that captures some variable like this

example.cpp
[&myVar] (/* ... */) {/* ... */}

&myVar means “capture myVar by reference”.

In most cases you can just capture all local variables by using a capture default:

example.cpp
[&] (/* ... */) {/* ... */}

In rare cases this will have unintended side-effects as you now are capturing all variables by reference where you might want to capture some by copy - so be sure to check your code.

Note that this error is GCC version-dependent. For me using GCC 7.2 fixed the error.


Check out similar posts by category: C/C++, GCC Errors