Inferred Type for Explicit Cast

eles via Digitalmars-d digitalmars-d at puremagic.com
Mon Dec 29 15:05:23 PST 2014


On Sunday, 21 December 2014 at 08:23:58 UTC, Jonathan Marler 
wrote:
> On Sunday, 21 December 2014 at 03:04:05 UTC, Steven 
> Schveighoffer wrote:
>> On 12/20/14 3:36 AM, Jonathan Marler wrote:
>>

> int y;
> ushort x = cast(byte, int)y; // cast from int to byte
>
> This should definitely produce an error.  This code could exist 
> if x was initially a byte and was later changed to a ushort. I 
> think this feature would make casts alot safer because any time 
> a type was changed you would get an error saying you need to go 
> update all your casts. What do you think?

This kinda helps for the other idea that I was mentioning: 
dissociate the data format/size of the access type when casting.

I still find very ugly, in C++, to specify a type to cast to for 
const_cast. It exposes the same problem, when one type change, 
you remain with an invalid cast.

In C is the same, and even worse. You have to specify that the 
cast is performed to remove "const"-ness, for example, in *the 
comments*. If the type change, you have an invalid cast, but the 
compiler will swallow it happily.

Should have "access_cast". That will only throw away const and 
let the type format/size unchanged. Implicit casting (or another 
explicit cast) will have to play the game to ensure type 
compatibility, not the remaining (kinda of) reinterpret_cast.



More information about the Digitalmars-d mailing list