Compile time check for GC?

Steven Schveighoffer schveiguy at gmail.com
Tue Jan 26 15:30:09 UTC 2021


On 1/26/21 8:10 AM, Ola Fosheim Grøstad wrote:
> Is there some way for library authors to test whether a GC is present at 
> compile time? @nogc just means that my code does not depend on a GC, but 
> it doesn't tell the compiler that my code is incompatible with GC.
> 
> I want to compute pointers in a way that is not scannable by a 
> conservative collector for performance reasons. So I need to make sure 
> that the faster code isn't chosen when people use GC using "static if"s.
> 
> 
> 

The only way to ensure the GC isn't used is with betterC. Even with 
@nogc tag on main, the GC could be used in static ctors, and casting 
function pointers/etc.

In that case, you can do version(D_BetterC)

And this will only apply to templates, not to compiled code, since 
compiled code already is done (and one can obviously use betterC 
compiled code in normal D code).

-Steve


More information about the Digitalmars-d-learn mailing list