8

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.

informaticienzero
  • 1,796
  • 1
  • 12
  • 22
  • I think your versions of g++ and Visual C++ would be relevant information. – molbdnilo Oct 11 '18 at 11:12
  • 1
    Out of curiosity, I put your code into godbolt with newest gcc (and compiled with `-O2`): [Test on godbolt](https://gcc.godbolt.org/z/Rb_D0Q). It seems that all these `const` variables are simply optimized away (using their values directly). May be, this fact let your gcc think there is something unnecessary in your lambda but I doubt it is right. – Scheff's Cat Oct 11 '18 at 11:13
  • @molbdnilo : I added them. – informaticienzero Oct 11 '18 at 11:26
  • So, does it mean that Visual Studio is not respecting the standard and should allow integer and boolean capture implicitly ? – informaticienzero Oct 11 '18 at 11:26
  • @informaticienzero, yes, MSVC should behave like gcc & clang, look at http://eel.is/c++draft/expr.const#2.13 – ixSci Oct 11 '18 at 11:30
  • Change them all to `constexpr` and you'll likely get a warning for all three. – StoryTeller - Unslander Monica Oct 11 '18 at 11:56
  • @informaticienzero, are you going to report the issue to MSVC team? If so please add the issue link to the question, if not let me know. I'll do it myself. – ixSci Oct 11 '18 at 12:48
  • 2
    @ixSci : I submitted the issue, the link is there : https://developercommunity.visualstudio.com/content/problem/355230/cannot-have-implicit-const-variables-captures-with.html – informaticienzero Oct 11 '18 at 13:25
  • @informaticienzero the link to the original question is empty – Shafik Yaghmour Oct 11 '18 at 16:41

0 Answers0