如果我使用任何
-O
标志,以下简单的 C++ 代码将无法使用 g++ 进行编译。我不明白为什么。
最小且可重现的示例:
#include <vector>
using A = std::vector<size_t>;
A foo(const A& a)
{
if (a.size() < 2)
return a;
return {};
}
int main()
{
A obj;
foo(obj);
}
编译标志:
-Ofast -Werror
您可以使用 GCC 13.2.0 在 godbolt 上测试完整的编译器输出。 相关编译错误:
/opt/compiler-explorer/arm/gcc-trunk-20240202/arm-unknown-linux-gnueabihf/arm-unknown-linux-gnueabihf/include/c++/14.0.1/bits/stl_algobase.h:438:30: error: 'void* __builtin_memmove(void*, const void*, unsigned int)' writing between 5 and 2147483647 bytes into a region of size 4 overflows the destination [-Werror=stringop-overflow=]
438 | __builtin_memmove(__result, __first, sizeof(_Tp) * _Num);
| ~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~