Casting away const and invariant

Janice Caron caron800 at googlemail.com
Mon Sep 10 00:17:09 PDT 2007


>From the sneaky tricks department...

You know, is it actually possible to circumvent const/invariant correctness
*without using a cast at all*. Of course, as with cirucumvention by any
other means, this will lead to undefined behaviour, but, just for a laugh...


string s = "cat"; /* invariant */

/* now let's modify the string, without using cast */
char * p;
p += &s[0] - null;

/* drum roll... */

*p = 'b';
writefln(s); /* writes "bat" on Windows */



Never, ever do this! :-)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20070910/1ba2e560/attachment.html>


More information about the Digitalmars-d mailing list