Bug 11328 - userdefined implicit typecast returns wrong reference
Summary: userdefined implicit typecast returns wrong reference
Status: RESOLVED DUPLICATE of bug 11329
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 3.3
: P2 normal
Target Milestone: 3.4.0
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2003-06-26 13:33 UTC by Jan Starzynski
Modified: 2005-07-23 22:49 UTC (History)
1 user (show)

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


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Jan Starzynski 2003-06-26 13:33:21 UTC
In the appended code the implicit typecast to Base& does not return a reference to 
the correct object but to a temporary instead. The typecast to Derived& works. For 
gcc 2.95.3 and Borland C++ the code is ok. It is compiled with "g++ bugsim1.cc". 
 
g++ -v: 
Reading specs from /usr/lib/gcc-lib/i486-suse-linux/3.3/specs 
Configured with: ../configure --enable-threads=posix --prefix=/usr 
--with-local-prefix=/usr/local --infodir=/usr/share/info --mandir=/usr/share/man 
--libdir=/usr/lib --enable-languages=c,c++,f77,objc,java,ada --disable-checking 
--enable-libgcj --with-gxx-include-dir=/usr/include/g++ --with-slibdir=/lib 
--with-system-zlib --enable-shared --enable-__cxa_atexit i486-suse-linux 
Thread model: posix 
gcc version 3.3 20030226 (prerelease) (SuSE Linux) 
 
uname -a: 
Linux earth 2.4.20-4GB #1 Wed Jun 18 07:45:45 UTC 2003 i686 unknown unknown 
GNU/Linux 
 
The system is a standard SuSE Linux 8.2 on Pentium 4. 
 
************************************* 
 
#include <iostream> 
 
using namespace std; 
 
class Base { 
}; 
 
class Derived: public Base { 
}; 
 
class Super { 
public: 
  Derived *derived; 
 
  Super(): derived(new Derived) {} 
  ~Super() { delete derived; } 
 
  Base *pointer() const { return derived; } 
 
  operator Base &() const { return *derived; } 
  operator Derived &() const { return *derived; } 
}; 
 
void equalConst(const Base &base, const Derived &derived, const Base *orig) 
{ 
  cerr << "base    == orig: " << (&base == orig) << " (expected 1)" << endl; 
  cerr << "derived == orig: " << (&derived == orig) << " (expected 1)" << endl; 
} 
 
int main() 
{ 
  Super super; 
  equalConst(super, super, super.pointer()); 
}
Comment 1 Wolfgang Bangerth 2003-06-26 14:36:06 UTC
Confirmed with 3.2, but this is already fixed in 3.3 and mainline.
W.
Comment 2 Andrew Pinski 2003-06-26 14:41:39 UTC
An almost exact dup of 11329 (say for extra code in this one).

*** This bug has been marked as a duplicate of 11329 ***