Don wrote:
Walter Bright wrote:
3. used with operator overloading to convert a user defined type to
its preferred arithmetic representation (a cast can't know what the
'preferred' type is)
> 5. to coerce default integral promotion rules (again, cast(int) won't
> always produce the same result)
This one is interesting, and might be the strongest argument, but I
don't understand it. An example would be interesting.
Integral promotion rules:
byte b;
+b => int
dchar dc;
+dc => uint
long l;
+l => long
cast(int)l => Oops! lost some bits
user defined type:
struct S
{
long opCast() { ... }
S opAdd(ref S) { ... }
}
S s;
+s => long result
s + s => S result
s + +s => long result
s + cast(int)s => Oops! lost some bits!