Const template

Andrei Alexandrescu (See Website For Email) SeeWebsiteForEmail at erdani.org
Tue Jan 23 16:39:14 PST 2007


Chris Nicholson-Sauls wrote:
> Andrei Alexandrescu (See Website For Email) wrote:
>> Chris Nicholson-Sauls wrote:
>>> So then I ask, why have const constructors/destructors?  What, given 
>>> an (even contrived) example, would be the likely difference in code 
>>> between constant and non-constant constructors/destructors?  And how 
>>> is this:
>>
>> A constructor might take different decisions on caching, preallocating
>> etc. when it prepares for mutability, as opposed to it knowing that the
>> object won't change. For example, creating a const deque might cause a 
>> different allocation strategy than a non-const one.
> 
> Okay, granted.  (Const instances coming from a pool rather than newly 
> created, for example.)  So, a new question, can the const constructors 
> call the "normal" constructors?  Aka:
> 
> class Foo {
>   this () { /* ... do stuff ... */ }
> 
>   this(const) () {
>     // ... yadda yadda
>     this();
>     // ... yadda yadda
>   }
> }

Yes. Also, it should be notice that a const constructor could escape a 
non-const reference to this, which makes the tree falling in the forest 
audible. This is in keep with the intended semantics of const, and 
testament to the fact that you don't want a more restrictive semantics.

> And what else might possible gain this odd 'func(attr)(...)' notation 
> later?  Its... interesting, to be sure.  But certainly better to my eyes 
> than 'func (...) const' and company ever were.

Scope is another interesting attribute that you may want to detect.


Andrei



More information about the Digitalmars-d mailing list