为什么我只为bool和int得到-Wunused-lambda-capture而不是double?

inf*_*ero 8 c++ lambda c++17

我实际上在Visual Studio 2017(Visual C++ 14.15.26706)上用C++ 17中的lambdas进行了一些测试.下面的代码非常有效.

#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;
}
Run Code Online (Sandbox Code Playgroud)

但是我的一个朋友在Qt Creator上测试了一个现代的MinGW,我也做了,我们都得到两个相同代码的警告而不是珍贵.

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]
Run Code Online (Sandbox Code Playgroud)

如果我删除a_boolean,并an_integer从捕捉区域,该代码仍然使用MinGW编译,甚至在网上像Wandbox(GCC 9.0)与-pedantic和C++ 17,但现在不是了与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.
Run Code Online (Sandbox Code Playgroud)

如果我删除const从的声明an_integera_boolean,在所有平台上我确实需要捕捉他们,如果我不这样做,任何人都不会编译.

如果我删除a_real,MinGW和GCC都不高兴并且不想编译,抱怨error: 'a_real' is not captured,即使它是const.

  • 哪个编译器是对的?MinGW/GCC还是Visual Studio?
  • 为什么GCC允许隐式捕获布尔值和整数,但为什么禁止浮点值呢?
  • 有哪些规则或解释可以帮助我更好地了解正在发生的事情?

谢谢大家,祝大家度过美好的一天.