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] New: Copy constructor is not called
Date: Fri, 28 May 2004 22:10:00 -0000	[thread overview]
Message-ID: <20040528101645.15704.justin@cs.utah.edu> (raw)

In the following snippet of code, the copy constructor is not called on the
indicated line (in main()).  Bug 8053 indicates that temporary objects can be
elided, but it's not clear how far this behavior extends.  Refer to the
following code:

#include <stdio.h>
 
class Test
{
public:
        Test (unsigned i0) : i (i0)
        {
                printf ("Standard constructor\n");
        }
 
        Test (const Test & src) :
                i (src.i)
        {
                printf ("Copy constructor\n");
        }
 
        Test create (unsigned i0)
        {
                Test retval (i0);
 
                return retval;
        }
 
        void show ()
        {
                printf ("Value: %u\n", i);        }
 
private:
        unsigned i;
 
};
 
int main (int, char **)
{
        Test c (0);
 
        Test b (c.create (1));  \\ HERE!!
 
        c.show ();
        b.show ();
 
        return 0;
}

The line indicated in main() should call create and then call Test's copy
constructor.  This example was discovered while attempting to work with a
reference-counting class that relies on the copy constructor being called, even
with a temporary object.

MS VC6 and .net as well as the SGI C++ compiler all call the copy constructor on
the indicated line.

Some extra info:
$ uname -aLinux unreal 2.4.20-20.9 i686 i686 i386 GNU/Linux

$ gcc -v
Reading specs from /usr/lib/gcc-lib/i386-redhat-linux/3.2.2/specs
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man
--infodir=/usr/share/info --enable-shared --enable-threads=posix
--disable-checking --with-system-zlib --enable-__cxa_atexit --host=i386-redhat-linux
Thread model: posix
gcc version 3.2.2 20030222 (Red Hat Linux 3.2.2-5)

I've also tried this on gcc 3.3.3 to the same effect.

If this is not a bug, please cite the section of the C++ standard which allows
this behavior.

Thanks

-- 
           Summary: Copy constructor is not called
           Product: gcc
           Version: 3.3.3
            Status: UNCONFIRMED
          Severity: normal
          Priority: P2
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: justin at cs dot utah dot edu
                CC: gcc-bugs at gcc dot gnu dot org


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


             reply	other threads:[~2004-05-28 10:16 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-05-28 22:10 justin at cs dot utah dot edu [this message]
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
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=20040528101645.15704.justin@cs.utah.edu \
    --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).