public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/15704] New: Copy constructor is not called
@ 2004-05-28 22:10 justin at cs dot utah dot edu
  2004-05-28 22:24 ` [Bug c++/15704] " reichelt at gcc dot gnu dot org
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: justin at cs dot utah dot edu @ 2004-05-28 22:10 UTC (permalink / raw)
  To: gcc-bugs

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


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2004-06-06  1:05 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-05-28 22:10 [Bug c++/15704] New: Copy constructor is not called 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
2004-06-06  1:05 ` pinskia at gcc dot gnu dot org

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).