Hi,
Consider Allocators, e.g.:
struct Mallocator
{
enum usesGC = false;
/// implement alloc, free, etc. @nogc
}
struct GCAllocator
{
enum usesGC = true;
/// implement alloc, free, etc. via the GC
}
Now I want to have the function attributes set depending on the allocator implementation
template AutoGC(ALLOCATOR) if (isAllocator!ALLOCATOR)
{
static if (!ALLOCATOR.usesGC)
AutoGC = pragma(attrib, @nogc);
else
AutoGC = pragma(attrib, none);
}
@AutoGC!ALLOCATOR void fun(ALLOCATOR)() if(isAllocator!ALLOCATOR)
{
void* p = ALLOCATOR.alloc(1024);
// do something with p
ALLOCATOR.free(p);
}
So fun!Mallocator would be @nogc and fun!GCAllocator wouldn't be @nogc.
This is a contrived example. In reality I would use this with custom array, hash map and other container implementations so I could use them in @nogc territory by just switching out the allocator.
Is it possible to do something like this ?