www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Attribute inference within template functions

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));
}
Apr 22 2020