This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug c++/15704] New: Copy constructor is not called


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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]