https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66093
Bug ID: 66093 Summary: g++ produces incorrect output on code with constexpr function initializing class with private fields Product: gcc Version: 5.1.1 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: denvned at gmail dot com Target Milestone: --- Compiling the following code using command "g++ -std=c++14 main.cpp" produces an incorrect result: #include <iostream> constexpr int n = 10; struct A { constexpr operator const int*() const { return data; } constexpr operator int*() { return data; } private: int data[n]; }; constexpr A f() { A a{}; for (int i = 1; i <= n; i++) { a[i] = i; } return a; } A a = f(); int main() { for (int i = 0; i < n; i++) { std::cout << a[i] << std::endl; } } Expected output of execution of this code is a list of numbers from 1 to 10. But the program produced by GCC outputs a list of zeroes: http://coliru.stacked-crooked.com/a/414c82777a938a80 The produced assembly code is clearly incorrect: https://goo.gl/UJvcOJ I have found that: - If "private:" is removed, or - if "constexpr" is removed from "f()", or - if "A a = f();" is moved into the body of "main()", or - if an user defined constructor initializing "data" is added, then the bug disappears... Clang produces correct output on this code. I hope this information will help you fix the bug.