Convert const(GUID)* to GUID*
Jonathan M Davis
jmdavisProg at gmx.com
Sat Jun 25 17:23:04 PDT 2011
On 2011-06-25 14:41, Jesse Phillips wrote:
> 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.
Ah yes. You're right I misremembered. Casting away const is defined and legal,
but _modifying_ such a variable is not (unlike C++).
- Jonathan M Davis
More information about the Digitalmars-d-learn
mailing list