Topic: Does casting a pointer create a temporary?


Author: checker@acf3.nyu.edu (Christopher Hecker)
Date: 9 Jun 92 00:02:14 GMT
Raw View
>05: void foo ( const T*& t)
>10: main()
>11: {
>12: T *bar = new T;
>15:   foo(bar);

Comeau 3.0 compiles this without warning or error.  BC++ 3.0 warns that
it has created a temporary for bar.  I don't know why it would create a
temp, though...casting bar to a (T const *) eliminates the temporary.

Chris




Author: richard@stat.tamu.edu (richard henderson)
Date: Tue, 9 Jun 1992 12:49:41 GMT
Raw View
eeh@public.BTR.COM (Eduardo E. Horvath  eeh@btr.com) writes:


>I am currently having an argument with DEC over the usability of `const'.
>
>Let us assume the following source:

>05: void foo ( const T*& t)
>06: {
>07: // does something, but does not modify *t
>08: }

>I also maintain that casting a pointer merely changes its type and
>does not call a conversion function or create a temporary. [ARM p69:
>"Operations performed on the result of a pointer or reference cast
>refer to the same object as the original (uncast) expression."]
>
>The DEC cxx compiler (and DEC) claim that this is not so.  It
>complains in the first instance (which g++-2.1 accepts gracefully)
>that:
>
>:15:error: In this statement, the initialization of a non-const reference
>requires a temporary for "bar".
>
>If I attempt to cast the pointer as above I get:
>
>:15: error: In this statement, the initialization of a non-const reference
>requires a temporary for "(const T ...)bar".

The problem is that the reference to the variable is not constant.
You should try declaring foo() as

 void foo(const T * const & t)

assuming that the reference should really be constant.  This will
eliminate the "initialization of a non-const reference" error.
There is no point, though, in using a reference to a pointer if the
pointer is not to be modified -- just pass the pointer directly.

r~
(richard@stat.tamu.edu)





Author: eeh@public.BTR.COM (Eduardo E. Horvath eeh@btr.com)
Date: 8 Jun 92 15:11:33 GMT
Raw View
I am currently having an argument with DEC over the usability of `const'.

Let us assume the following source:

01: class T {
02: // contains something
03: };
04:
05: void foo ( const T*& t)
06: {
07: // does something, but does not modify *t
08: }
09:
10: main()
11: {
12: T *bar = new T;
13:
14:  for(int i=0; i<20; i++) {
15:   foo(bar);
16:   // Do something that modifies bar
17:  }
18: }


Now I presume that because `bar' is a pointer to a `T', not an
instance of a `T', the reference will not be built properly, and
an error will occur.

I also assert that this can be remedied by explicitly casting `bar'
to a `(const T*)' like so:

15:  foo((const T*)bar);

I also maintain that casting a pointer merely changes its type and
does not call a conversion function or create a temporary. [ARM p69:
"Operations performed on the result of a pointer or reference cast
refer to the same object as the original (uncast) expression."]

The DEC cxx compiler (and DEC) claim that this is not so.  It
complains in the first instance (which g++-2.1 accepts gracefully)
that:

:15:error: In this statement, the initialization of a non-const reference
requires a temporary for "bar".

If I attempt to cast the pointer as above I get:

:15: error: In this statement, the initialization of a non-const reference
requires a temporary for "(const T ...)bar".

The solution DEC talls me to use is to create a temporary  pointer for
the funcion call:

14.1:  const T *temp;
14.1:  temp = (const T *)bar;
15:             foo(temp);

Assuming that foo modifies the pointer, I must also add:

15.1:  bar = (T *)temp;

Is this really the true interpretation of the standard?  What is the point
using const, which is supposed to help the programmer by protecting objects
from being modified, if it is so painful to use?


--
=========================================================================
Eduardo Horvath    eeh@btr.com
     ..!{decwrl,mips,fernwood}!btr!eeh
 "Trust me, I am cognizant of what I am doing." - Hammeroid