www.digitalmars.com         C & C++   DMDScript  

D - Cast operator

reply C.R.Chafer <blackmarlin nospam.asean-mail.com> writes:
I was considering that there are really two froms of the cast operator

type 1.

Alter bit pattern converting to a new type.

        int i = 42;
        float f = cast(float) i;

type 2.

Retain bit pattern - override language typing rules.

        MyClass c = new ...
        OtherClass o = cast( OtherClass ) c;

(This latter form is almost always none portable)

Should there two formats use different syntaxes - ie for the second type use

        OtherClass o = override_type( OtherClass ) c;

(keyword debateable - could even overload typedef?)

Because we could for example want to convert a float to a long integer 
while retaining the bit pattern - using a union would be a problem due to 
implementation specific alignment concerns and there seems to be no simple 
way to go about this.

Comments?

C 2002/8/9
Aug 09 2002
parent reply "Walter" <walter digitalmars.com> writes:
You're right about the two uses. Usually for the "type 2" cast (which I call
"painting") I'll do something like:

    float f = *cast(float *)cast(int *)(&i);

It's ugly, but then again, it should be something rarely necessary. (And
shouldn't ugly hacks be ugly to look at? <g>)

"C.R.Chafer" <blackmarlin nospam.asean-mail.com> wrote in message
news:aj09j1$1r9u$1 digitaldaemon.com...
 I was considering that there are really two froms of the cast operator

 type 1.

 Alter bit pattern converting to a new type.

         int i = 42;
         float f = cast(float) i;

 type 2.

 Retain bit pattern - override language typing rules.

         MyClass c = new ...
         OtherClass o = cast( OtherClass ) c;

 (This latter form is almost always none portable)

 Should there two formats use different syntaxes - ie for the second type
use
         OtherClass o = override_type( OtherClass ) c;

 (keyword debateable - could even overload typedef?)

 Because we could for example want to convert a float to a long integer
 while retaining the bit pattern - using a union would be a problem due to
 implementation specific alignment concerns and there seems to be no simple
 way to go about this.

 Comments?

 C 2002/8/9
Aug 09 2002
next sibling parent reply C.R.Chafer <blackmarlin nospam.asean-mail.com> writes:
Walter wrote:

 You're right about the two uses. Usually for the "type 2" cast (which I
 call "painting") I'll do something like:
 
     float f = *cast(float *)cast(int *)(&i);
 
 It's ugly, but then again, it should be something rarely necessary. (And
 shouldn't ugly hacks be ugly to look at? <g>)
Your are right about ugly - though I suppose this works (guess this is [the] one situation where a C sytle macro would be a good solution ie. #define cast_float( a, b ) *(float*)(a*)(&b) ). Maybe adding this to the documentation (near the section on casts) would be a good idea. In a future version of D maybe properties could be added to the basic types to allow this operation. C 2002/8/10
Aug 10 2002
parent "Sean L. Palmer" <seanpalmer earthlink.net> writes:
You can always make an inline function:

float int_to_float(int i) { return *cast(float *)&i; }

Since a cast is entirely compile time operation, the compiler should decide
to inline the function since it consists of one dereference, which a
function call would make about 3x more expensive.

Why would you need the cast(int*) there?

And who cares if it's ugly.  So long as it's possible.  It's something that
should be difficult to do by accident.

Sean


"C.R.Chafer" <blackmarlin nospam.asean-mail.com> wrote in message
news:aj348j$23la$1 digitaldaemon.com...
 Walter wrote:

 You're right about the two uses. Usually for the "type 2" cast (which I
 call "painting") I'll do something like:

     float f = *cast(float *)cast(int *)(&i);

 It's ugly, but then again, it should be something rarely necessary. (And
 shouldn't ugly hacks be ugly to look at? <g>)
Your are right about ugly - though I suppose this works (guess this is [the] one situation where a C sytle macro would be a good solution ie. #define cast_float( a, b ) *(float*)(a*)(&b) ). Maybe adding this to the documentation (near the section on casts) would
be
 a good idea.
 In a future version of D maybe properties could be added to the basic
types
 to allow this operation.

 C 2002/8/10
Aug 11 2002
prev sibling parent reply "Sandor Hojtsy" <hojtsy index.hu> writes:
"Walter" <walter digitalmars.com> wrote in message
news:aj10o0$2k24$1 digitaldaemon.com...
 You're right about the two uses. Usually for the "type 2" cast (which I
call
 "painting") I'll do something like:

     float f = *cast(float *)cast(int *)(&i);
Why not float f = *cast(float *)(&i);
Aug 26 2002
parent "Walter" <walter digitalmars.com> writes:
"Sandor Hojtsy" <hojtsy index.hu> wrote in message
news:akcof8$vt6$1 digitaldaemon.com...
 "Walter" <walter digitalmars.com> wrote in message
 news:aj10o0$2k24$1 digitaldaemon.com...
 You're right about the two uses. Usually for the "type 2" cast (which I
call
 "painting") I'll do something like:

     float f = *cast(float *)cast(int *)(&i);
Why not float f = *cast(float *)(&i);
You're right.
Aug 26 2002