On Tuesday, 19 June 2018 at 21:38:14 UTC, Jonathan M Davis wrote:
Attribute inference is all about inferring the attributes of the templated function based on its implementation, not adjusting its implementation based on where it's used.

Indeed, I realised that the same template instantiation can't possibly have both a @nogc and @gc version, so there has to be a template parameter. You can automatically set one however:


```
void main() @nogc {
    example();
}

void example(string callee = __FUNCTION__)() {

    int a = 8;
    static if (isNoGc!callee) {
        printf("printf %d", a);
    } else {
        writefln("writeln %d", a);
    }
}

bool isNoGc(string functionName)() {
    import std.algorithm;
mixin(`return [__traits(getFunctionAttributes, `~functionName~`)].canFind("@nogc");`);
}
```

This seems to work. I'm not sure how robust it is though. You might need an extra `mixin("import "~functionName~";");` in `isNoGc` when it's called from other modules (if that helps at all).

Reply via email to