d的扩大@nogc
import std.traits;
// 从函数或闭包类型中转换`@nogc`
auto assumeNoGC(T) (T t) if (isFunctionPointer!T || isDelegate!T)
{
enum attrs = functionAttributes!T | FunctionAttribute.nogc;
return cast(SetFunctionAttributes!(T, functionLinkage!T, attrs)) t;
}
//不能按`@nogc`标记函数,但是你知道不会使用垃集
void funcThatMightUseGC(int timeout)
{
if (unlikelyCondition(timeout))
throw new Exception("已抛");
doMoreStuff();
}
void funcThatCantAffortGC() @nogc
{
// 用转换闭包调用垃集.
assumeNoGC( (int timeout)
{
funcThatMightUseGC(timeout);
})(10000);
}