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] Copy constructor is not called


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


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