https://gcc.gnu.org/bugzilla/show_bug.cgi?id=106131
Bug ID: 106131 Summary: -fstrict-aliasing breaks normal program that does not use any pointer or reference Product: gcc Version: 12.1.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: 18307130172 at fudan dot edu.cn Target Milestone: --- Target: x86_64-pc-linux-gnu Created attachment 53220 --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=53220&action=edit preprocessed source file My program does not use any pointer or reference but is affected by -fstrict-aliasing. Here is a minimal example to manifest this issue: #include <vector> #include <iostream> using Pair = std::pair<int, int>; std::vector<Pair> f = {{0, -11}, {0, -8}, {1, 2}}; int foo(Pair x, Pair y) { return std::max(x.second, y.second); } int main() { for (int J = 0; J < 1; J++) { f[J] = f[0]; if(J == 0) f[J] = f[2]; std::cout << foo(f[J], f[1]) << std::endl; } } Save it to main.cpp and compile it with: g++ -O2 main.cpp The expected output should be "2", but the optimized build output "-8". If compiled with "-O2 -fno-strict-aliasing", the output is correct.