* Reference to pointer and cast - is this a bug ?
@ 2006-12-01 9:15 Philippe Combes
2006-12-01 12:46 ` John Love-Jensen
0 siblings, 1 reply; 2+ messages in thread
From: Philippe Combes @ 2006-12-01 9:15 UTC (permalink / raw)
To: gcc-help
Hi all,
Here is code that works exactly as I think it should with g++-2.95, but
not with
g++-4.0/4.1
#include <iostream>
using namespace std;
class parent
{
public:
parent():i(0){}
parent(int i_):i(i_){}
private:
int i;
};
class child : public parent
{
public:
child():parent() {}
child(int i_):parent(i_){}
};
void trace( const parent* const& p_ptr )
{
cout << "trace called on a reference tp pointer @"
<< (void*)&p_ptr << ": pointer = " << (void*)p_ptr << endl;
}
int main( int argc, char** argv )
{
const child c(4);
const child* c_ptr = &c;
const child* const& c_ptr_r = c_ptr;
cout << "Call trace on a reference to pointer @"
<< (void*)&c_ptr_r << ": pointer = " << (void*)c_ptr_r <<
endl;
trace( c_ptr_r );
return 0;
}
Output with g++-2.95 (which I think is right)
Call trace on a reference to pointer @0xbfb43a40: pointer = 0xbfb43a44
trace called on a reference tp pointer @0xbfb43a40: pointer = 0xbfb43a44
Output with g++-4.0 or 4.1
Call trace on a reference to pointer @0xbfac899c: pointer = 0xbfac89a0
trace called on a reference tp pointer @0xbfac89a4: pointer = 0xbfac89a0
which means that the pointer passed by reference to trace has been
copied, just
as if it was passed by value.
If this is a bug, I did not find it in the bug database.
If not, can anybody tell me why ?
Thanks in advance,
Philippe
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: Reference to pointer and cast - is this a bug ?
2006-12-01 9:15 Reference to pointer and cast - is this a bug ? Philippe Combes
@ 2006-12-01 12:46 ` John Love-Jensen
0 siblings, 0 replies; 2+ messages in thread
From: John Love-Jensen @ 2006-12-01 12:46 UTC (permalink / raw)
To: Philippe Combes, MSX to GCC
Hi Philippe,
> If this is a bug, I did not find it in the bug database.
> If not, can anybody tell me why ?
Not a bug.
A child is a parent.
A child pointer, itself, is a type.
A parent pointer, itself, is a type.
A child pointer is not a parent pointer.
Put ten more trace(c_ptr_r); in your source. Notice that each one has a
different address. Anonymous variables are being created - anonymous
variables that have a different type.
Take out the const part of the const& to trace, and you'll notice that the
compiler indicates that is an error - because a child pointer is not a
parent pointer.
Since you used a const&, the child pointer will be treated as if it is a
parent pointer (via the magic of the anonymous variable introduced and
implicit conversion).
The trace statement is printing the address of the anonymous variable, which
is why it differs.
May have something to do with the scaffolding for the SSA optimization, even
in non-optimized code (-O0).
HTH,
--Eljay
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2006-12-01 12:46 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-12-01 9:15 Reference to pointer and cast - is this a bug ? Philippe Combes
2006-12-01 12:46 ` John Love-Jensen
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).