How to attach function attributes to delegate type?

Alex sascha.orlov at
Wed Feb 27 20:45:33 UTC 2019

On Wednesday, 27 February 2019 at 20:03:15 UTC, Q. Schroll wrote:
> For any type constructors like const, I can use ConstOf!T to 
> get `T` with const attached. For a delegate/function type DG, 
> e.g. int delegate(int), how can I get the @safe version of that 
> type, i.e. int delegate(int) @safe?
> I tried
>     alias SafeOf(DG) = DG @safe;
> but it didn't compile.
> The case is not @safe-specific; it's the same for all function 
> attributes.


there is a demonstration for @nogc:

import std.traits;

// Casts @nogc out of a function or delegate type.
auto assumeNoGC(T) (T t) if (isFunctionPointer!T || isDelegate!T)
     enum attrs = functionAttributes!T | FunctionAttribute.nogc;
     return cast(SetFunctionAttributes!(T, functionLinkage!T, 
attrs)) t;

Didn't try this for other cases, however...

More information about the Digitalmars-d-learn mailing list