Bypassing the postblit?
monarch_dodra
monarchdodra at gmail.com
Sun Dec 29 11:22:50 PST 2013
On Sunday, 29 December 2013 at 18:40:07 UTC, Maxim Fomin wrote:
> On Sunday, 29 December 2013 at 16:22:04 UTC, Ritu wrote:
>> I have a struct that wraps a class object and lazily
>> initializes it. Now in case the struct instance is passed as
>> an argument to a function and it has not been initialized yet,
>> the default copy constructor and the postblit do not offer a
>> possibility to initialize the class object before copying.
>
> Why this is a problem? You can create function which return
> class field from struct wrapper and make such function alias
> this, in addition postblit should allocate new class. The fact
> that original struct may have null value is irrelevant if
> copying is made correctly.
It's a problem for "reference semantic structs" that need to be
initialized. This is actually a "well know" and often encountered
problem.
It strikes things like Appende, Array, and also built in AA's.
void foo(Appender!(int[]) app)
{
app.put(1);
}
void main()
{
Appender!(int[]) app1;
Appender!(int[]) app2 = appender!(int[]);
foo(app1);
foo(app2);
assert(app1.data == [1]); //Fails
assert(app2.data == [1]); //Passes
}
More information about the Digitalmars-d
mailing list