http://gcc.gnu.org/bugzilla/show_bug.cgi?id=50776
Bug #: 50776 Summary: unused object optimized out, despite having constructor Classification: Unclassified Product: gcc Version: 4.6.1 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ AssignedTo: unassig...@gcc.gnu.org ReportedBy: n...@math.technion.ac.il Consider the following simple program: #include <cstdio> class Ident { public: Ident(const char *ident){ // This constructor prints a message! printf("yo\n"); } }; static Ident id("$Id: hello $"); main(){ printf("hello\n"); } When you compile it with g++ without additional parameters, its output rightly looks like: yo hello I.e., the object "id" gets instantiated, and its constructor prints the message "yo". However, if you compile it with optimization enabled - g++ -O2 - the output is DIFFERENT: the "id" object is optimized out (because it is static, but nobody uses it in this source file), and therefore its constructor no longer runs. I think it is a bug for optimization to change the behavior (not just the performance) of a program. I think that if an object has a non-trivial constructor with any side effects beside setting class fields, then we cannot optimize its construction out because this would change the program behavior.