Copy constructors for lazy initialization

Michel Fortin michel.fortin at michelf.com
Sat May 29 04:40:12 PDT 2010


On 2010-05-28 21:26:50 -0400, Andrei Alexandrescu 
<SeeWebsiteForEmail at erdani.org> said:

> Walter has had a great idea last night: allow classes to define
> 
> this(ref S src);
> 
> where S is the name of the struct being defined, as an alternative for
> 
> this(this);
> 
> The result would be a function similar with a C++ copy constructor.
> 
> Such an abstraction allows us to perform lazy initialization in a way 
> that allows the kind of problems associated with non-shared hash tables:

At this point I'll put the lazy initialization into question. If as 
soon as you make a copy of the struct you must allocate, it means that 
the container will be initialized as soon as you pass it to some 
function (unless the argument is passed by 'ref', but you want 
reference semantics precisely to avoid that, am I right?).

If the container is to be initialized as soon as you make a copy, the 
lazy initialization becomes of limited utility; it'll only be useful 
when you have a container you don't pass to another function *and* you 
never put anything in it. This makes the tradeoff of lazy 
initialization less worth it, as the extra logic checking every time if 
the container is already initialized will rarely serve a purpose.


-- 
Michel Fortin
michel.fortin at michelf.com
http://michelf.com/



More information about the Digitalmars-d mailing list