Transitive const sucks

James Dennett jdennett at acm.org
Thu Sep 13 19:56:06 PDT 2007


Walter Bright wrote:
> James Dennett wrote:
>> Walter Bright wrote:
>>
>>> Or you can cast away const-ness. But you are on your own if you do that.
>>
>> Well, in C++ you'd be fine, but in D I thought the behavior
>> if you modified something after casting away const was
>> undefined, so you're out of luck.  (Not that you'd need
>> to cast away const for this in C++.)
> 
> It's also undefined behavior if you cast a pointer to an int. You need
> to know what you're doing.

Right.  But you've snipped too much context.  The point was that
C++ allows something, and D doesn't.  In D you can't even cast
away the const and then modify the underlying object, because
it's undefined (so if you do so, you *don't* know what you're
doing).

The need is to modify state in an object associated with another
object.  That doesn't change the object in question (either
physically or logically) but is disallowed by D, as there's
no recognition that association does not imply aggregation/
containment.

-- James



More information about the Digitalmars-d mailing list