public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "nyh at math dot technion.ac.il" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c++/50776] New: unused object optimized out, despite having constructor
Date: Tue, 18 Oct 2011 14:10:00 -0000	[thread overview]
Message-ID: <bug-50776-4@http.gcc.gnu.org/bugzilla/> (raw)

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.


             reply	other threads:[~2011-10-18 14:10 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-10-18 14:10 nyh at math dot technion.ac.il [this message]
2011-10-18 14:19 ` [Bug c++/50776] " redi at gcc dot gnu.org
2011-10-18 15:47 ` nyh at math dot technion.ac.il

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-50776-4@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).