Casting away const

BCS none at anon.com
Mon Aug 9 08:32:59 PDT 2010


Hello Steven,

> On Mon, 09 Aug 2010 10:53:48 -0400, BCS <none at anon.com> wrote:
> 
>>> C's api can be modified at declaration.  It has no mangling, so you
>>> can  type it how it should be (if C had const).  For example:
>>> extern(C) int strlen(const(char)* str);
>>> I find that much more pleasant than having to cast away const.
>> OTOH that is effectively a hidden cast and has 100% of the same
>> issues  (re undefined behavior) as casting away const while being
>> slightly  harder to find.
>> 
> But you just said that casting and reading is not undefined?  Isn't
> this  the same thing?

Casting away const or tacking const into a extern(C) prototype is safe under 
exactly the same conditions. The gains a savings of a few keystrokes and 
looses some degree of findability, aside from that, there is effectively 
no difference between them.

> if you are sure
> a  const or immutable piece of data is on the heap/stack, it should be
> reasonable to be able to modify it for performance gains.

A conforming compiler can be implemented in such a way that you can never 
be sure of that without looking at the generated asm or even in such a way 
that it can't be know till runtime.

-- 
... <IXOYE><





More information about the Digitalmars-d-learn mailing list