fake RefCounted for CTFE?

Steven Schveighoffer schveiguy at gmail.com
Tue Jun 16 00:23:19 UTC 2020


On 6/15/20 5:39 PM, Timon Gehr wrote:
> On 15.06.20 23:18, Steven Schveighoffer wrote:
>> I'm doing some stuff with RefCounted for a factory-like expression, 
>> where I expect the factory harnesses are going to be very short-lived. 
>> However, these don't work at CTFE (because malloc/etc aren't available).
>>
>> Would it make sense for RefCounted to turn into GC for CTFE mode? That 
>> is, if RefCounted is used during CTFE, it just allocates on the GC 
>> heap and doesn't actually do ref counting. The end result will be a 
>> ref counted struct that somehow is flagged that it's never going away 
>> (and it shouldn't, if it's generated at compile time).
>>
>> Then you could use functions that return RefCounted items as static 
>> initializers, or use libraries that use RefCounted at compile time 
>> without issues.
>>
>> What do you think?
> 
> I think it should work in CTFE, but it cannot break @nogc.

Damn you're right. I can't think of a good way around that.

All I can think of is a specialized function which allocates only in 
CTFE, and the compiler pretends it can be @nogc.

-Steve


More information about the Digitalmars-d mailing list