I'm actually doing some tests with lambdas in C++17, on Visual Studio 2017 (Visual C++ 14.15.26706). The code below is perfectly working.
#include <iostream>
int main()
{
bool const a_boolean { true };
int const an_integer { 42 };
double const a_real { 3.1415 };
auto lambda = [a_boolean, an_integer, a_real]() -> void
{
std::cout << "Boolean is " << std::boolalpha << a_boolean << "." << std::endl;
std::cout << "Integer is " << an_integer << "." << std::endl;
std::cout << "Real is " << a_real << "." << std::endl;
};
lambda();
return 0;
}
But a friend of mine tested on Qt Creator with a modern MinGW, and I did too, and we both get two warnings for the same code than preciously.
warning: lambda capture 'a_boolean' is not required to be capture for this use. [-Wunused-lambda-capture]
warning: lambda capture 'an_integer' is not required to be capture for this use. [-Wunused-lambda-capture]
If I delete a_boolean
and an_integer
from the capture zone, the code still compiles with MinGW, and even online like on Wandbox (GCC 9.0) with -pedantic
and C++17, but not anymore with Visual Studio 2017.
C3493 'a_boolean' cannot be implicitly captured because no default capture mode has been specified.
C3493 'an_integer' cannot be implicitly captured because no default capture mode has been specified.
C2064 term does not evaluate to a function taking 0 arguments.
If I delete const
from the declarations of an_integer
and a_boolean
, on all platforms I do need to capture them and if I don't, any of them won't compile.
If I delete a_real
, MinGW and GCC are both unhappy and don't want to compile, complaining that error: 'a_real' is not captured
, even if it's const
.
- Which compiler is right ? MinGW/GCC or Visual Studio ?
- Why do GCC allows implicit capture for a boolean and an integer, but why is it forbidden for a floating-point value ?
- What are some rules or explanations that can help me better understand what's happening ?
Thanks everyone, I wish a fantastic day to all of you.