Struct Constructor Lazy

Jiyan via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Jul 12 04:34:45 PDT 2017


On Wednesday, 12 July 2017 at 11:18:08 UTC, Biotronic wrote:
> On Wednesday, 12 July 2017 at 11:00:54 UTC, Jiyan wrote:
>> [...]
>
> The traditional solution is static opCall:
>
> struct A {
>     int field;
>     static A opCall() {
>         A result;
>         result.field = getDataFromFile("file.txt");
>         return result;
>     }
> }
>
> A instance = A();
>
> I believe I've heard this is frowned upon these days, but I 
> don't know of a better solution.
>
> For optimal speed you might also want to skip default 
> initialization of result, by writing A result = void;.
>
> I would be surprised if the optimizer wasn't able to optimize 
> away the useless parameter though - have you looked at the 
> generated assembly?
>
> --
>   Biotronic

Hey,
yes i did but to be honest i used dmd in debug version.
The thing about the static one, is that it creates a local object 
A isnt that a performance issue itself - or am i wrong - im 
confused actually :P?


More information about the Digitalmars-d-learn mailing list