https://gcc.gnu.org/bugzilla/show_bug.cgi?id=68998
Bug ID: 68998 Summary: Wrong code generated regarding constexpr arrays Product: gcc Version: 5.3.1 Status: UNCONFIRMED Severity: critical Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: likan_999.student at sina dot com Target Milestone: --- The following code generates wrong result: #include <iostream> using namespace std; template <typename T, int N> struct ArrayHolder { T v[N]; }; constexpr ArrayHolder<int, 2> makeArray() { ArrayHolder<int, 2> a{}; a.v[1] = 1; a.v[0] = 1; return a; } constexpr ArrayHolder<int, 2> arr = makeArray(); int main() { cout << arr.v[0] << endl; for (int i = 0; i < 2; i++) { for (int j = 0; j < arr.v[i]; j++) { cout << "i = " << i << ", j = " << j << endl; } } } Result: [hidden] g++ -std=c++14 c.cpp && ./a.out 1 i = 1, j = 0 Expected Result: 1 i = 0, j = 0 i = 1, j = 0 I am using priority 'critical' because it generates wrong code, silently, so it is very bad.