Attribute inference within template functions

jmh530 john.michael.hall at gmail.com
Wed Apr 22 14:07:19 UTC 2020


I was trying to write a function has different behavior depending 
on whether it is called from @nogc code or not. However, I am 
noticing that this does not seem possible because of the timing 
of attribute inference.

If I call getFunctionAttributes within foo below, then it is 
system but then when called from main they are correctly 
inferred. It is as if the attribute inference happens after the 
getFunctionAttributes is called.

Is there any way to get the correct function attributes within a 
template function?

auto foo(T)(T x) {
     pragma(msg, __traits(getFunctionAttributes, foo!T));
     pragma(msg, __traits(getFunctionAttributes, foo!int));
     return x;
}

void main() {
     auto x = foo(1);
     pragma(msg, __traits(getFunctionAttributes, foo!int));
}


More information about the Digitalmars-d-learn mailing list