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

Basile B. b2.temp at gmx.com
Wed Jun 3 13:24:17 UTC 2020


On Wednesday, 3 June 2020 at 10:24:44 UTC, Simen Kjærås wrote:
> On Wednesday, 3 June 2020 at 09:39:34 UTC, Basile B. wrote:
>> You can use this template:
>>
>>   enum Exists(alias T) = is(typeof(T));
>>
>> I don't know if there's a faster way bu this technic is used, 
>> notatbly in phobos, to workaroud issues of double declaration 
>> in `static foreach`
>
> enum Exists(alias T) = is(typeof(T));
> static assert(!Exists!bar); // undefined identifier bar
>
> --
>   Simen

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;
}
---


More information about the Digitalmars-d-learn mailing list