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 09:39:34 UTC 2020


On Wednesday, 3 June 2020 at 09:03:22 UTC, drathier wrote:
> I'm generating some code. Some of the generated types need to 
> be overridden, so I define them manually at the top of the 
> generated file. Then I need to guard against redefining the 
> identifier (type/value/function) later on, in the generated 
> code.
>
> I'm currently using `static if (!__traits(compiles, thingy)) {` 
> to avoid redefining things twice.
>
> Of course the proper fix is to not generate code for the 
> identifiers which are already manually defined, and not 
> generate any `static if`s at all, but until then, is there a 
> faster way than `static if (__traits(compiles, ...` to check if 
> a type/value/function has already been defined?

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`


More information about the Digitalmars-d-learn mailing list