D - Cast operator
- C.R.Chafer (19/19) Aug 09 2002 I was considering that there are really two froms of the cast operator
- Walter (8/27) Aug 09 2002 You're right about the two uses. Usually for the "type 2" cast (which I ...
- C.R.Chafer (10/17) Aug 10 2002 Your are right about ugly - though I suppose this works (guess this is
- Sean L. Palmer (13/30) Aug 11 2002 You can always make an inline function:
- Sandor Hojtsy (5/8) Aug 26 2002 call
- Walter (3/13) Aug 26 2002 You're right.
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
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 typeuseOtherClass 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
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
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:beYou'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) woulda good idea. In a future version of D maybe properties could be added to the basictypesto allow this operation. C 2002/8/10
Aug 11 2002
"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 Icall"painting") I'll do something like: float f = *cast(float *)cast(int *)(&i);Why not float f = *cast(float *)(&i);
Aug 26 2002
"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.You're right about the two uses. Usually for the "type 2" cast (which Icall"painting") I'll do something like: float f = *cast(float *)cast(int *)(&i);Why not float f = *cast(float *)(&i);
Aug 26 2002