public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/14140] New: Unwanted call to derived constructor after implicit conversion
@ 2004-02-13 14:08 christian dot engstrom at glindra dot org
2004-02-13 14:36 ` [Bug c++/14140] " gcc-bugs at michaelmellor dot com
` (9 more replies)
0 siblings, 10 replies; 11+ messages in thread
From: christian dot engstrom at glindra dot org @ 2004-02-13 14:08 UTC (permalink / raw)
To: gcc-bugs
I posted essentially the following to comp.lang.c++, where it was suggested that
I report this as a possible gcc bug:
--------
When i compile the program listed below with gcc version 3.3.1 (MinGW on
Windows XP) I get the following result:
Calling 'func(d)':
'base' copy constructor
Calling 'func(*d_handle)':
'base' copy constructor
Calling 'func(d_handle)':
'derived' copy constructor <-- Why?
'base' copy constructor
The problem is the call to the 'derived' copy constructor, which I don't want
and hadn't expected, and which Microsoft Visual C++ 6.0 doesn't generate.
Is it the gcc or the MSVC compiler that is broken? I would naively have assumed
that MSVC is right and gcc is wrong in this case, but perhaps there is something
in the standard that prescribes the gcc behavior for some reason?
My intention was that there shouldn't be any temporary 'derived' objects created
at all, since both the implicit conversion operator from 'derived_handle' to
'derived' and 'derived's operator* return references to an existing 'derived'
object.
------------
In the comp.lang.c++ newsgroup
lilburne wrote:
>
> It looks like a gcc bug. The compiler is treating the returned derived&
> differently when it is returned from the conversion function
>
> operator derived&()
>
> to what it does when it calls dereference function:
>
> derived& operator*()
>
> even if operator derived&() is rewritten in the clearer form
>
> operator derived&() const {return operator*();}
>
> the extra constructor is called.
>
--------------
The output from the command gcc -v looks like this:
Reading specs from c:/app/mingw/bin/../lib/gcc-lib/mingw32/3.3.1/specs
Configured with: ../gcc/configure --with-gcc --with-gnu-ld --with-gnu-as
--host=mingw32 --target=mingw32 --prefix=/mingw --enable-threads --disable-nls
--enable-languages=c,c++,f77,objc,ada,java --disable-win32-registry
--disable-shared --enable-sjlj-exceptions --enable-libgcj --disable-java-awt
--without-x --enable-java-gc=boehm --disable-libgcj-debug --enable-interpreter
--enable-hash-synchronization
Thread model: win32
gcc version 3.3.1 (mingw special 20030804-1)
-------------
The program that causes the problem:
=======================================================================
#include <iostream>
//----------------------------------------------------------------------------
class base
{
public:
base() {}
base(const base&) {std::cout << " 'base' copy constructor" << std::endl;}
};
//----------------------------------------------------------------------------
class derived : public base
{
public:
derived() {}
derived(const derived&) {std::cout << " 'derived' copy constructor" <<
std::endl;}
};
//----------------------------------------------------------------------------
class derived_handle
{
derived* tptr;
public:
derived_handle() {};
derived_handle(derived* inptr) : tptr(inptr) {}
derived& operator*() const {return *tptr;}
derived* operator->() const {return tptr;}
operator derived&() const {return **this;} // Implicit conversion to 'derived'
};
//----------------------------------------------------------------------------
void func(base b) {} // Takes the parameter by value
//============================================================================
int main (int argc, char* argv[])
{
derived d;
derived_handle d_handle(new derived());
std::cout << "\nCalling 'func(d)':" << std::endl;
func(d);
std::cout << "\nCalling 'func(*d_handle)':" << std::endl;
func(*d_handle);
std::cout << "\nCalling 'func(d_handle)':" << std::endl;
func(d_handle); // Calls 'derived' copy constructor with gcc (only)
return 0;
}
--
Summary: Unwanted call to derived constructor after implicit
conversion
Product: gcc
Version: 3.3.1
Status: UNCONFIRMED
Severity: normal
Priority: P2
Component: c++
AssignedTo: unassigned at gcc dot gnu dot org
ReportedBy: christian dot engstrom at glindra dot org
CC: gcc-bugs at gcc dot gnu dot org
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=14140
^ permalink raw reply [flat|nested] 11+ messages in thread
* [Bug c++/14140] Unwanted call to derived constructor after implicit conversion
2004-02-13 14:08 [Bug c++/14140] New: Unwanted call to derived constructor after implicit conversion christian dot engstrom at glindra dot org
@ 2004-02-13 14:36 ` gcc-bugs at michaelmellor dot com
2004-02-13 14:55 ` pinskia at gcc dot gnu dot org
` (8 subsequent siblings)
9 siblings, 0 replies; 11+ messages in thread
From: gcc-bugs at michaelmellor dot com @ 2004-02-13 14:36 UTC (permalink / raw)
To: gcc-bugs
--
What |Removed |Added
----------------------------------------------------------------------------
CC| |gcc-bugs at michaelmellor
| |dot com
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=14140
^ permalink raw reply [flat|nested] 11+ messages in thread
* [Bug c++/14140] Unwanted call to derived constructor after implicit conversion
2004-02-13 14:08 [Bug c++/14140] New: Unwanted call to derived constructor after implicit conversion christian dot engstrom at glindra dot org
2004-02-13 14:36 ` [Bug c++/14140] " gcc-bugs at michaelmellor dot com
@ 2004-02-13 14:55 ` pinskia at gcc dot gnu dot org
2004-02-14 20:11 ` pinskia at gcc dot gnu dot org
` (7 subsequent siblings)
9 siblings, 0 replies; 11+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2004-02-13 14:55 UTC (permalink / raw)
To: gcc-bugs
------- Additional Comments From pinskia at gcc dot gnu dot org 2004-02-13 14:55 -------
ICC 6.0 just calls derived::derived(derived const&) instead base::base(base const&).
I do not know who is right now.
--
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=14140
^ permalink raw reply [flat|nested] 11+ messages in thread
* [Bug c++/14140] Unwanted call to derived constructor after implicit conversion
2004-02-13 14:08 [Bug c++/14140] New: Unwanted call to derived constructor after implicit conversion christian dot engstrom at glindra dot org
2004-02-13 14:36 ` [Bug c++/14140] " gcc-bugs at michaelmellor dot com
2004-02-13 14:55 ` pinskia at gcc dot gnu dot org
@ 2004-02-14 20:11 ` pinskia at gcc dot gnu dot org
2004-02-28 14:52 ` giovannibajo at libero dot it
` (6 subsequent siblings)
9 siblings, 0 replies; 11+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2004-02-14 20:11 UTC (permalink / raw)
To: gcc-bugs
------- Additional Comments From pinskia at gcc dot gnu dot org 2004-02-14 20:11 -------
Confirmed, related to 14035.
--
What |Removed |Added
----------------------------------------------------------------------------
Status|UNCONFIRMED |NEW
Ever Confirmed| |1
Keywords| |rejects-valid, wrong-code
Last reconfirmed|0000-00-00 00:00:00 |2004-02-14 20:11:43
date| |
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=14140
^ permalink raw reply [flat|nested] 11+ messages in thread
* [Bug c++/14140] Unwanted call to derived constructor after implicit conversion
2004-02-13 14:08 [Bug c++/14140] New: Unwanted call to derived constructor after implicit conversion christian dot engstrom at glindra dot org
` (2 preceding siblings ...)
2004-02-14 20:11 ` pinskia at gcc dot gnu dot org
@ 2004-02-28 14:52 ` giovannibajo at libero dot it
2004-04-28 3:40 ` pinskia at gcc dot gnu dot org
` (5 subsequent siblings)
9 siblings, 0 replies; 11+ messages in thread
From: giovannibajo at libero dot it @ 2004-02-28 14:52 UTC (permalink / raw)
To: gcc-bugs
------- Additional Comments From giovannibajo at libero dot it 2004-02-28 14:52 -------
I don't see any reason why the derived copy constructor should be called. I
also double-checked with MSVC71 and Comeau 4.3.3 (which uses the latest EDG),
and they both agree that only the base constructor should be called.
It looks like the conversion operator is responsible for this behaviour, and
calls the copy constructor withour realizing the operator* returns the same
type.
Can anybody check if this is a regression? I don't have anything older than 3.2
(and yes, I should really rebuild those).
--
What |Removed |Added
----------------------------------------------------------------------------
Known to fail| |3.3.3 3.2.3 3.4.0 3.5.0
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=14140
^ permalink raw reply [flat|nested] 11+ messages in thread
* [Bug c++/14140] Unwanted call to derived constructor after implicit conversion
2004-02-13 14:08 [Bug c++/14140] New: Unwanted call to derived constructor after implicit conversion christian dot engstrom at glindra dot org
` (3 preceding siblings ...)
2004-02-28 14:52 ` giovannibajo at libero dot it
@ 2004-04-28 3:40 ` pinskia at gcc dot gnu dot org
2004-04-28 11:30 ` giovannibajo at libero dot it
` (4 subsequent siblings)
9 siblings, 0 replies; 11+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2004-04-28 3:40 UTC (permalink / raw)
To: gcc-bugs
------- Additional Comments From pinskia at gcc dot gnu dot org 2004-04-28 02:06 -------
Well I made the derived copy construtor private and I noticed that ICC errors out that the
copy construtor is inaccessible.
pr14140.cc
pr14140.cc(56): error #373: "derived::derived(const derived &)" is inaccessible
func((d_handle)); // Calls 'derived' copy constructor with gcc (only)
^
pr14140.cc(56): error #373: "derived::derived(const derived &)" is inaccessible
func((d_handle)); // Calls 'derived' copy constructor with gcc (only)
^
compilation aborted for pr14140.cc (code 2)
GCC does the thing, so this is not wrong-code at all as it is allowed by the standard to
call the copy construtor here.
--
What |Removed |Added
----------------------------------------------------------------------------
Status|NEW |RESOLVED
Resolution| |INVALID
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=14140
^ permalink raw reply [flat|nested] 11+ messages in thread
* [Bug c++/14140] Unwanted call to derived constructor after implicit conversion
2004-02-13 14:08 [Bug c++/14140] New: Unwanted call to derived constructor after implicit conversion christian dot engstrom at glindra dot org
` (4 preceding siblings ...)
2004-04-28 3:40 ` pinskia at gcc dot gnu dot org
@ 2004-04-28 11:30 ` giovannibajo at libero dot it
2004-10-13 13:40 ` pinskia at gcc dot gnu dot org
` (3 subsequent siblings)
9 siblings, 0 replies; 11+ messages in thread
From: giovannibajo at libero dot it @ 2004-04-28 11:30 UTC (permalink / raw)
To: gcc-bugs
------- Additional Comments From giovannibajo at libero dot it 2004-04-28 11:22 -------
No, this *is* a bug. I already analyzed this, Andrew.
Try adding this:
derived& convert() const { return **this;}
to derived_handle, and using it instead of relying on the implicit conversion.
You will see that the call to derived construction is gone.
--
What |Removed |Added
----------------------------------------------------------------------------
Status|RESOLVED |REOPENED
Resolution|INVALID |
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=14140
^ permalink raw reply [flat|nested] 11+ messages in thread
* [Bug c++/14140] Unwanted call to derived constructor after implicit conversion
2004-02-13 14:08 [Bug c++/14140] New: Unwanted call to derived constructor after implicit conversion christian dot engstrom at glindra dot org
` (5 preceding siblings ...)
2004-04-28 11:30 ` giovannibajo at libero dot it
@ 2004-10-13 13:40 ` pinskia at gcc dot gnu dot org
2004-10-13 14:21 ` [Bug c++/14140] [3.3/3.4/4.0 Regression] " giovannibajo at libero dot it
` (2 subsequent siblings)
9 siblings, 0 replies; 11+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2004-10-13 13:40 UTC (permalink / raw)
To: gcc-bugs
--
What |Removed |Added
----------------------------------------------------------------------------
Status|REOPENED |NEW
Known to fail|3.3.3 3.2.3 3.4.0 4.0 |3.3.3 3.2.3 3.4.0 4.0.0
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=14140
^ permalink raw reply [flat|nested] 11+ messages in thread
* [Bug c++/14140] [3.3/3.4/4.0 Regression] Unwanted call to derived constructor after implicit conversion
2004-02-13 14:08 [Bug c++/14140] New: Unwanted call to derived constructor after implicit conversion christian dot engstrom at glindra dot org
` (6 preceding siblings ...)
2004-10-13 13:40 ` pinskia at gcc dot gnu dot org
@ 2004-10-13 14:21 ` giovannibajo at libero dot it
2004-10-13 14:25 ` giovannibajo at libero dot it
2004-10-13 14:59 ` pinskia at gcc dot gnu dot org
9 siblings, 0 replies; 11+ messages in thread
From: giovannibajo at libero dot it @ 2004-10-13 14:21 UTC (permalink / raw)
To: gcc-bugs
------- Additional Comments From giovannibajo at libero dot it 2004-10-13 14:21 -------
Slightly cleaned-up testcase:
--------------------------------------------------
extern "C" int printf(const char* fmt, ...);
extern "C" void abort(void);
struct A {
A() {}
A(const A&) { printf("A cctor\n"); }
};
struct B : public A {
B() {}
B(const B&) { printf("B cctor\n"); abort(); }
};
struct Wrapper
{
B* b;
Wrapper(B* b_) : b(b_) {}
B& operator*() const {return *b;}
operator B&() const {return **this;}
};
void func(A a) {} // Takes the parameter by value
int main()
{
B b;
Wrapper b_wrap(&b);
printf("\nCalling 'func(b)':\n");
func(b);
printf("\nCalling 'func(*b_wrap)':\n");
func(*b_wrap);
printf("\nCalling 'func(b_wrap)':\n");
func(b_wrap);
return 0;
}
--------------------------------------------------
In short, this should not abort, and does not with ICC, Comeau, or MSVC. This
is a regression since 2.95 appeared with 3.0.
--
What |Removed |Added
----------------------------------------------------------------------------
Severity|normal |critical
Known to fail|3.3.3 3.2.3 3.4.0 4.0.0 |3.0.4 3.3.3 3.2.3 3.4.0
| |4.0.0
Known to work| |2.95.3
Summary|Unwanted call to derived |[3.3/3.4/4.0 Regression]
|constructor after implicit |Unwanted call to derived
|conversion |constructor after implicit
| |conversion
Target Milestone|--- |3.4.3
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=14140
^ permalink raw reply [flat|nested] 11+ messages in thread
* [Bug c++/14140] [3.3/3.4/4.0 Regression] Unwanted call to derived constructor after implicit conversion
2004-02-13 14:08 [Bug c++/14140] New: Unwanted call to derived constructor after implicit conversion christian dot engstrom at glindra dot org
` (7 preceding siblings ...)
2004-10-13 14:21 ` [Bug c++/14140] [3.3/3.4/4.0 Regression] " giovannibajo at libero dot it
@ 2004-10-13 14:25 ` giovannibajo at libero dot it
2004-10-13 14:59 ` pinskia at gcc dot gnu dot org
9 siblings, 0 replies; 11+ messages in thread
From: giovannibajo at libero dot it @ 2004-10-13 14:25 UTC (permalink / raw)
To: gcc-bugs
------- Additional Comments From giovannibajo at libero dot it 2004-10-13 14:25 -------
The original thread (with some C++ standard reference) is here:
http://tinyurl.com/5smcv
--
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=14140
^ permalink raw reply [flat|nested] 11+ messages in thread
* [Bug c++/14140] [3.3/3.4/4.0 Regression] Unwanted call to derived constructor after implicit conversion
2004-02-13 14:08 [Bug c++/14140] New: Unwanted call to derived constructor after implicit conversion christian dot engstrom at glindra dot org
` (8 preceding siblings ...)
2004-10-13 14:25 ` giovannibajo at libero dot it
@ 2004-10-13 14:59 ` pinskia at gcc dot gnu dot org
9 siblings, 0 replies; 11+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2004-10-13 14:59 UTC (permalink / raw)
To: gcc-bugs
------- Additional Comments From pinskia at gcc dot gnu dot org 2004-10-13 14:59 -------
Then this is a dup of bug 14035.
*** This bug has been marked as a duplicate of 14035 ***
--
What |Removed |Added
----------------------------------------------------------------------------
Status|NEW |RESOLVED
Resolution| |DUPLICATE
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=14140
^ permalink raw reply [flat|nested] 11+ messages in thread
end of thread, other threads:[~2004-10-13 14:59 UTC | newest]
Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-02-13 14:08 [Bug c++/14140] New: Unwanted call to derived constructor after implicit conversion christian dot engstrom at glindra dot org
2004-02-13 14:36 ` [Bug c++/14140] " gcc-bugs at michaelmellor dot com
2004-02-13 14:55 ` pinskia at gcc dot gnu dot org
2004-02-14 20:11 ` pinskia at gcc dot gnu dot org
2004-02-28 14:52 ` giovannibajo at libero dot it
2004-04-28 3:40 ` pinskia at gcc dot gnu dot org
2004-04-28 11:30 ` giovannibajo at libero dot it
2004-10-13 13:40 ` pinskia at gcc dot gnu dot org
2004-10-13 14:21 ` [Bug c++/14140] [3.3/3.4/4.0 Regression] " giovannibajo at libero dot it
2004-10-13 14:25 ` giovannibajo at libero dot it
2004-10-13 14:59 ` 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).