作者:wing96333 | 来源:互联网 | 2022-12-02 15:18
我实际上在Visual Studio 2017(Visual C++ 14.15.26706)上用C++ 17中的lambdas进行了一些测试.下面的代码非常有效.
#include
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 " <
但是我的一个朋友在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]
如果我删除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.
如果我删除const
从的声明an_integer
和a_boolean
,在所有平台上我确实需要捕捉他们,如果我不这样做,任何人都不会编译.
如果我删除a_real
,MinGW和GCC都不高兴并且不想编译,抱怨error: 'a_real' is not captured
,即使它是const
.
哪个编译器是对的?MinGW/GCC还是Visual Studio?
为什么GCC允许隐式捕获布尔值和整数,但为什么禁止浮点值呢?
有哪些规则或解释可以帮助我更好地了解正在发生的事情?
谢谢大家,祝大家度过美好的一天.