考虑以下代码:
#include <iostream>
#include <functional>
std::function<void ()> f()
{
int x = 666;
return [&] { std::cout << x << std::endl; };
}
int main()
{
f()();
return 0;
}
在Ubuntu 18.04 bionic(WSL)上使用GCC 7.5.0进行编译:没有优化
$ g++ -o main -std=c++14 -Wall main.cpp
$ ./main
666
-O1
$ g++ -o main -O1 -std=c++14 -Wall main.cpp
$ ./main
0
-O2
$ g++ -o main -O2 -std=c++14 -Wall main.cpp
main.cpp: In function ‘int main()’:
main.cpp:7:31: warning: ‘x’ is used uninitialized in this function [-Wuninitialized]
return [&] { std::cout << x << std::endl; };
^
$ ./main
32699
-O3
$ g++ -o main -O3 -std=c++14 -Wall main.cpp
main.cpp: In function ‘int main()’:
main.cpp:7:31: warning: ‘x’ is used uninitialized in this function [-Wuninitialized]
return [&] { std::cout << x << std::endl; };
^
$ ./main
32528
在Windows 10 x64上使用TDM-GCC 9.2.0进行编译:没有优化
>g++ -o main.exe -std=c++14 -Wall main.cpp
>.\main.exe
666
-O1
>g++ -o main.exe -O1 -std=c++14 -Wall main.cpp
>.\main.exe
0
-O2
>g++ -o main.exe -O2 -std=c++14 -Wall main.cpp
>.\main.exe
0
-O3
>g++ -o main.exe -O3 -std=c++14 -Wall main.cpp
>.\main.exe
0
在Windows 10 x64上使用MSVC 19.27.29111进行编译:没有优化
>cl /EHsc main.cpp
Microsoft (R) C/C++ Optimizing Compiler Version 19.27.29111 for x86
Copyright (C) Microsoft Corporation. All rights reserved.
main.cpp
Microsoft (R) Incremental Linker Version 14.27.29111.0
Copyright (C) Microsoft Corporation. All rights reserved.
/out:main.exe
main.obj
>.\main.exe
8402693
/O1
>cl /EHsc /O1 main.cpp
Microsoft (R) C/C++ Optimizing Compiler Version 19.27.29111 for x86
Copyright (C) Microsoft Corporation. All rights reserved.
main.cpp
Microsoft (R) Incremental Linker Version 14.27.29111.0
Copyright (C) Microsoft Corporation. All rights reserved.
/out:main.exe
main.obj
>.\main.exe
666
/O2
>cl /EHsc /O2 main.cpp
Microsoft (R) C/C++ Optimizing Compiler Version 19.27.29111 for x86
Copyright (C) Microsoft Corporation. All rights reserved.
main.cpp
Microsoft (R) Incremental Linker Version 14.27.29111.0
Copyright (C) Microsoft Corporation. All rights reserved.
/out:main.exe
main.obj
>.\main.exe
666
如您所见,使用不同的编译器和优化级别,该程序将输出666
,0
或垃圾值。为什么会发生上述情况? 最佳答案
您可以通过lambda中的引用来捕获x
,并且在保留f()
后,由于x
被破坏,它变成了悬空的引用。您有一个经典的UB。为了避免这种情况,您可以通过编写x
或[x]
而不是[=]
来按值捕获[&]
。