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

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