This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug c++/50776] New: unused object optimized out, despite having constructor
- From: "nyh at math dot technion.ac.il" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: Tue, 18 Oct 2011 14:09:43 +0000
- Subject: [Bug c++/50776] New: unused object optimized out, despite having constructor
- Auto-submitted: auto-generated
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: unassigned@gcc.gnu.org
ReportedBy: nyh@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.