October 11, 2019 Fastest way to check if a predicate can take a single parameter of a specific type | ||||
---|---|---|---|---|
| ||||
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? |
October 11, 2019 Re: Fastest way to check if a predicate can take a single parameter of a specific type | ||||
---|---|---|---|---|
| ||||
Posted in reply to Per Nordlöw | 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))
|
Copyright © 1999-2021 by the D Language Foundation