public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "justin at cs dot utah dot edu" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c++/15704] Copy constructor is not called
Date: Sat, 05 Jun 2004 22:56:00 -0000	[thread overview]
Message-ID: <20040605225647.4909.qmail@sourceware.org> (raw)
In-Reply-To: <20040528101645.15704.justin@cs.utah.edu>


------- Additional Comments From justin at cs dot utah dot edu  2004-06-05 22:56 -------
(In reply to comment #1)
> Not a bug.
> 
> The compiler is allowed to optimize away calls to the copy constructor,
> even if the copy-construtor or the destructor have side-effects, see 12.8.15.
> 

Sorry if I'm being dense here, but I believe this to be an invalid resolution of
this bug.  I have read the indicated section of standard (albeit from the '96
draft of the standard) and it only allows a copy constructor to be ignored if
the object being passed is never referenced again, except by an implicit
destructor.  So, in the example above, the copy constructor is never called
because the result of create() is used in it's place.  However, if the copy
constructor were rewritten as

Test::Test (const Test & src) :
    i (0)
{
    printf ("Copy constructor\n");
}

(as contrived as it may be), then the result of running the program is
incorrect.  The expression Test b (c.create (1)) does not return an object whose
member variable i is equal to 0.  This has led to erroneous behavior in some of
my code.  If the compiler can prove that the copy constructor only creates an
exact copy of an object, then it is safe to equate the new object to the source
object and elide the call to the copy constructor, but in this case, that is not
correct behavior.  In this case, the value returned from create() does not need
to be copied, but the copy constructor must still be called and (as of gcc
3.3.3) it is not.

-- 
           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|RESOLVED                    |UNCONFIRMED
         Resolution|INVALID                     |


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=15704


  parent reply	other threads:[~2004-06-05 22:56 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-05-28 22:10 [Bug c++/15704] New: " justin at cs dot utah dot edu
2004-05-28 22:24 ` [Bug c++/15704] " reichelt at gcc dot gnu dot org
2004-06-05 22:56 ` justin at cs dot utah dot edu [this message]
2004-06-06  1:05 ` pinskia at gcc dot gnu dot org

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=20040605225647.4909.qmail@sourceware.org \
    --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).