Const template

Frits van Bommel fvbommel at REMwOVExCAPSs.nl
Tue Jan 23 09:13:25 PST 2007


Bruno Medeiros wrote:
> Andrei Alexandrescu (See Website For Email) wrote:
>> Bruno Medeiros wrote:
>>> Whoa, huh? What's the meaning of a const constructor or of a const 
>>> destructor? How would they even be invoked?
>>
>> class Widget { ... }
>> auto a = new Widget; // invokes this()
>> auto b = new const Widget; // invokes this(const)()
> 
> Ok, but what is such a const object? Is it an object that actually does 
> not change? (as opposed to simply a read-only view of it?)

I'd guess it would be an object with only read-only views (i.e. one that 
doesn't change between the end of the constructor and the beginning of 
the destructor).

> And what is the difference between a normal delete, and the deletion of 
> a const reference? (the latter doesn't even make sense to me, delete is 
> a mutating operation)

Deletion of a const reference will be necessary for const objects at the 
very least (since there are no non-const references to them). 
Presumably, that's what the "const destructor" is for.

A case could be made for disallowing explicit deletion of const 
references, but that would mean const objects would only be deletable by 
the GC. That, in turn would mean const objects would be unusable by 
people who need (or just prefer) the GC to be disabled...
Perhaps this should be more of a "best practice" instead of a language rule?



More information about the Digitalmars-d mailing list