Using .require for struct types

Erdem kerdemdemir at gmail.com
Tue Sep 13 06:10:49 UTC 2022


On Saturday, 10 September 2022 at 18:38:40 UTC, Ali Çehreli wrote:
> On 9/10/22 09:33, Erdem Demir wrote:
>
> >          DListOfA returnVal = temp.require("a",
> DListOfA());--> I wish I
> > could use ref DListOfA here
>
> But keeping a reference to a temporary would not work because 
> the life of that temporary ends by the end of that expression 
> (practically, at the semicolon).
>
> An option is to allocate the object dynamically with new (and 
> store DListOfA* in the associative array). Then the GC would 
> keep it alive as long its pointer was in the associative arrray.
>
> But a better option is to just forget about it because D 
> already takes care of rvalues by blitting (bit-level copying) 
> them by default. Everything just works... :) It is not 
> expensive either. For example, your struct is very cheap to 
> copy.
>
> But I am probably missing the reason why you want a ref there. 
> Perhaps there are even better options.
>
> Ali

In the code sample I posted I need returnVal.insert(a); have an 
effect on DListOfA[string] temp; but if returnVal is a rvalue 
copy I can't accomplish this right?

I see your point about ref would have been assign to a temp val, 
thanks for pointing out.




More information about the Digitalmars-d-learn mailing list