Partially initialized structs?

Hipreme msnmancini at hotmail.com
Thu Feb 27 01:22:07 UTC 2025


On Wednesday, 26 February 2025 at 23:47:41 UTC, Arredondo wrote:
> On Wednesday, 26 February 2025 at 10:19:17 UTC, Hipreme wrote:
>>
>>     ```d
>>     struct S
>>     {
>>         int[100] a;
>>         int b = 0;
>>         static S defaultInit(int b)
>>         {
>>            S s = void;
>>            s.b = b;
>>            return s;
>>         }
>>     }
>>     ```
>
> Thank you for this Hipreme. I do have a question though, 
> wouldn't this create two copies of S? one at construction and 
> then another at the call site after defaultInit returns?
>
> Regarding safety, I'm well aware I'm playing with fire here. 
> For now I'm just experimenting to see if something like this is 
> even worth it performance-wise.
>
> Cheers!
> Arredondo.

Yes, that is definitely worth performance wise.
Specially if you wish to start using an array of `S`, this could 
be a huge toll.

And no, it won't create 2 copies. D has a thing called Return 
Value Optimization. Since that local variable is returned, it has 
no need to copy it.




More information about the Digitalmars-d-learn mailing list