cast(!const) proposal

Steven Schveighoffer schveiguy at yahoo.com
Tue Sep 11 11:34:15 PDT 2007


"Janice Caron" wrote
> While I think of it, casting from const pointer to int should also be
> illegal without that same special syntax.
>
> For example
>
> const int n = 42;
> auto p = &n;
> auto k = cast(int)p; /* should be a compile error */
>
> Do we have intptr_t in D?
>
> Anyway, losing the constness by casting to int is cheating, so that
> too should require the special syntax.

I don't agree.  You are getting into Java-style hide-all-pointers 
terminology here.  If we are to have pointers, then I think we are going to 
have to agree that there will be ways to do *bad* things with them.  I can 
think of other ways to circumvent your const rule about casting to int that 
will allow code to change const data without having to use the cast(!const). 
I don't think it will be possible to get around all these possibilities with 
compiler errors.

My opinion is that any time you are casting away const, you should need the 
special cast(!const).  If you are doing funky things to get around that 
rule, then you are on your own.

patient: "doctor, it hurts when I do this!"
doctor: "then don't do that."

-Steve 





More information about the Digitalmars-d mailing list