Const transitivity is bad sometimes

Robert Fraser fraserofthenight at gmail.com
Tue Jan 15 21:21:40 PST 2008


naryl wrote:
> I've found several similar cases. It happens when short-lived invariant 
> object has reference to long-lived mutable object, but not the object 
> itself. When one object creates, contains and finalizes another it is 
> logical to make contained object invariant if container is invariant. 
> But when (as in example) invariant Message is used only to deliver 
> notifier to several subscribers, transitive const doesn't fit. Of course 
> I can cast it away, but on http://www.digitalmars.com/d/const3.html it 
> states that "modification after casting away const" [is] "undefined 
> behavior".

I think in this case it should be okay as long as at least one mutable 
reference to the object exists, since the referenced object can't be 
treated as invariant.


More information about the Digitalmars-d-learn mailing list