Const template
Andrei Alexandrescu (See Website For Email)
SeeWebsiteForEmail at erdani.org
Tue Jan 23 10:15:38 PST 2007
Frits van Bommel wrote:
> 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).
That is correct.
>> 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?
Deletion of pointers to const data has always been a sticky issue in
C++. There are good arguments either way. I think it should be
disallowed, otherwise a caller passing a const class object does not
have a guarantee that the callee didn't mutate the object, which is the
very purpose of the entire scaffolding.
Andrei
More information about the Digitalmars-d
mailing list