See this link, the "Known problems" section may help:
The thing about "aggressive optimisations are more likely to highlight undefined behaviour" is true if your compiler is perfect, and this may be almost true for well established compiler features, I don't believe this is the case of "-fgnu-tm", "-O3" or even "-std=c++11".
That is why this is not enabled by default.
Look what Gentoo's wiki says about "-O3":
"-O3: This is the highest level of optimization possible. It enables optimizations that are expensive in terms of compile time and memory usage. Compiling with -O3 is not a guaranteed way to improve performance, and in fact in many cases can slow down a system due to larger binaries and increased memory usage. -O3 is also known to break several packages. Therefore, using -O3 is not recommended."
Because of this, I am not sure the code is the problem or the only problem.
HTH,
Angelo