Fastest way to check using if identifier has already been defined, using static if or similar?

Paul Backus snarwin at gmail.com
Wed Jun 3 15:25:51 UTC 2020


On Wednesday, 3 June 2020 at 13:24:17 UTC, Basile B. wrote:
> This is because the template parameter must be resolved to a 
> valid symbol or type.
> This version other version bypass the problem:
>
> ---
> enum Exists(string s) = is(typeof(mixin(s)));
>
> void main()
> {
>     static if (!Exists!"foo")
>         int foo;
>     foo = 42;
> }
> ---

Fails if the symbol in question is the name of a type.

     struct Foo {}
     enum Exists(string s) = is(typeof(mixin(s)));
     static assert(Exists!"Foo"); // false

What you actually want is something like this:

     enum Exists(string s) = __traits(compiles, { mixin("alias _ = 
", s, ";"); });


More information about the Digitalmars-d-learn mailing list