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

* [Bug c++/15704] Copy constructor is not called
  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 ` 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
  2 siblings, 0 replies; 4+ messages in thread
From: reichelt at gcc dot gnu dot org @ 2004-05-28 22:24 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From reichelt at gcc dot gnu dot org  2004-05-28 11:27 -------
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.


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


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


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

* [Bug c++/15704] Copy constructor is not called
  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
  2 siblings, 0 replies; 4+ messages in thread
From: justin at cs dot utah dot edu @ 2004-06-05 22:56 UTC (permalink / raw)
  To: gcc-bugs


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


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

* [Bug c++/15704] Copy constructor is not called
  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
  2 siblings, 0 replies; 4+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2004-06-06  1:05 UTC (permalink / raw)
  To: gcc-bugs

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 1099 bytes --]


------- Additional Comments From pinskia at gcc dot gnu dot org  2004-06-06 01:05 -------
Not a bug, read 12.2P2
>From 12.2P2:
[Example: 
class X { 
//... 
public:
 //...
X(int);
 X(const X&); 
~X(); 
};
 X f(X); 
void g() { 
X a(1); 
X b = f(X(2)); 
a = f(a); 
}
 Here, an implementation might use a temporary in which to construct X(2) before passing it to f() using 
X’s copy-constructor; alternatively, X(2) might be constructed in the space used to hold the argument. 
Also, a temporary might be used to hold the result of f(X(2)) before copying it to b using X’s copy 
constructor; alternatively, f()’s result might be constructed in b. On the other hand, the expression 
a=f(a) requires a temporary for either the argumentaor the result of f(a) to avoid undesired aliasing of 
a. ] 

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


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