Using .require for struct types

Ali Çehreli acehreli at
Sat Sep 10 18:38:40 UTC 2022

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.


More information about the Digitalmars-d-learn mailing list