Fastest way to check if a predicate can take a single parameter of a specific type

Paul Backus snarwin at gmail.com
Fri Oct 11 15:04:54 UTC 2019


On Friday, 11 October 2019 at 09:05:25 UTC, Per Nordlöw wrote:
> I want to check whether
>
>     auto _ = pred(T.init);
>     static assert(is(typeof(_) == bool));
>
> compiles or not.
>
> Which one
>
>     __traits(compiles, { auto _ = pred(T.init); static 
> assert(is(typeof(_) == bool)); })
>
> and
>
>     is(typeof(pred(T.init)) == bool)
>
> is preferred compilation performance-wise?

Seems like the second one requires strictly less semantic 
analysis and is shorter and easier to read, so I'd go with that.

Note that `pred(T.init)` will fail if pred accepts its argument 
by ref. If you want to handle that case, you have to do something 
like

     is(ReturnType!((T arg) => pred(arg)) == bool))


More information about the Digitalmars-d-learn mailing list