Bug 28761 - Returning object ignores copy constructor
Summary: Returning object ignores copy constructor
Status: RESOLVED INVALID
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 3.4.3
: P3 critical
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2006-08-17 11:51 UTC by Alex S
Modified: 2006-08-17 14:48 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments
The original source code (214 bytes, text/plain)
2006-08-17 11:52 UTC, Alex S
Details
The preprocessed source (*.ii) file (90.92 KB, text/plain)
2006-08-17 11:54 UTC, Alex S
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Alex S 2006-08-17 11:51:35 UTC
Sytem information:
- LFS-based Linux, with kernel 2.6.15 (with some genpatches)

GCC configuring
---------------
./gcc-3.4.3/configure --prefix=/usr \
	--libexecdir=/usr/lib --enable-shared \
	--enable-threads=posix --enable-__cxa_atexit \
	--enable-clocale=gnu --enable-languages=c,c++

GCC Compiling:
-------------
make

bug description:
-----------------
We compiled the attached source code using the command:
 > g++ --save-temps build_and_destroy.cc -o bug

Running ./bug produced the following output:
A
A copy
A
~A
A.m:1
~A
~A

As you can see, returning the object in the function F didn't call the copy constructor. We expect the following output:
A
A copy
A
A copy
~A
~A
A.m:2
~A
~A
Comment 1 Alex S 2006-08-17 11:52:41 UTC
Created attachment 12088 [details]
The original source code

Bug related file
Comment 2 Alex S 2006-08-17 11:54:43 UTC
Created attachment 12089 [details]
The preprocessed source (*.ii) file

Bug related file
Comment 3 Pawel Sikora 2006-08-17 12:18:47 UTC
this is not a bug. the c++ standard allows RVO optimization.
see $12.8/15 for more details.
Comment 4 Alex S 2006-08-17 13:57:01 UTC
Never thought the C++ standard allows ignoring the code written in a dtor. What if the dtor does something important (like in the example - it doesn't just copy the contents)?
Comment 5 Alex S 2006-08-17 14:00:28 UTC
I meant the copy ctor, not the dtor.
Anyhow, looking at it again, it makes some sense.
Comment 6 Pawel Sikora 2006-08-17 14:11:39 UTC
(In reply to comment #5)
> I meant the copy ctor, not the dtor.
> Anyhow, looking at it again, it makes some sense.

"When certain criteria are met, an implementation is allowed to omit the copy
 construction of a class object, even if the copy constructor and/or
 destructor for the object have side effects.
 In such cases, the implementation treats the source and target
 of the omitted copy operation as simply two different ways of referring
 to the same object"

ps).
you can disable such optimization with -fno-elide-contructors
Comment 7 Andrew Pinski 2006-08-17 14:48:44 UTC
This is not a bug as mentioned by comment #6.