Const template

Frits van Bommel fvbommel at REMwOVExCAPSs.nl
Tue Jan 23 11:47:59 PST 2007


Andrei Alexandrescu (See Website For Email) wrote:
> Frits van Bommel wrote:
>> But as I mentioned, there will only be const references to const 
>> instances, so that would mean const objects couldn't be explicitly 
>> deleted.
>> And there are people who prefer or need to work with GC off. This 
>> limitation would deny them the use of const objects (unless they like 
>> memory leaks ;) ).
> 
> I disagree. If you want to disable the GC, that doesn't mean you can't 
> use const. You use a private non-const reference for ownership purposes, 
> and you use a const reference for publishing purposes:

<pedantic> I said const *objects*, not const references. </pedantic>

> Widget owned = new Widget;
> const Widget publicized = owned;
> ... use publicized wherever ...
> delete owned;
> 
> This is IMHO entirely reasonable.

Yeah, I guess it doesn't look that bad. Besides, I always keep the GC on 
anyway ;).

But I do think this is reasonably likely to become a FAQ.

>> For that reason, I think it should maybe merely be very rude to delete 
>> a const reference unless you are absolutely sure nobody else has a 
>> reference to the object (that they will ever use), not an error.
> 
> I think this might be an acceptable solution, but given the choice, I'd 
> stay away from it. I think the C++ experience hasn't shown delete const 
> to have any desirable virtues.
> 
> Imagine a dialog like this:
> 
> "You shot a diplomat with immunity!"
> "He's dead. A dead body can't have diplomatic immunity."
> "But he was alive and immune when you shot him!"
> "But at that point my bullet was in the gun."

LOL.

(Not sure it applies, but LOL none the less :) )



More information about the Digitalmars-d mailing list