Convert const(GUID)* to GUID*

Jesse Phillips jessekphillips+D at gmail.com
Sat Jun 25 14:41:26 PDT 2011


Jonathan M Davis Wrote:

> On 2011-06-25 13:05, Jimmy Cao wrote:
> > I think you can cast the const away.
> > cast(GUID*)&IID_IDirectInput8
> 
> Well, you _can_, but casting away const is not technically defined in D. It 
> should work at this point, but it's quite legal for a D compiler to _not_ work 
> with casting away const. You're really not supposed to be casting away const.

No, it is defined behavior: http://digitalmars.com/d/2.0/const3.html

"The immutable type can be removed with a cast"

"This does not mean, however, that one can change the data"

"The ability to cast away immutable-correctness is necessary in some cases where the static typing is incorrect and not fixable, such as when referencing code in a library one cannot change. Casting is, as always, a blunt and effective instrument, and when using it to cast away immutable-correctness, one must assume the responsibility to ensure the immutableness of the data, as the compiler will no longer be able to statically do so."

This is specific to immutable, but still applies to const. Casting away const/immutable is defined, modifying after the cast is not.


More information about the Digitalmars-d-learn mailing list