Testing whether static foreach is available

via Digitalmars-d digitalmars-d at puremagic.com
Sun Sep 10 03:39:57 PDT 2017


On Saturday, 9 September 2017 at 16:30:51 UTC, Jean-Louis Leroy 
wrote:
> Is there a way to test whether 'static foreach' is available, 
> e.g. via a version symbol?
>
> Also, is it possible to conditionally compile depending on the 
> compiler's version, not only the compiler (I mean, not compile 
> a block if it's GDC version 5.4.1 or lower)?
>
> I did look at the docs, several times, couldn't find it...

I would advice against checking the compiler version (as this is 
brittle) and instead check if the particular feature is really 
available.

enum hasFeatureAvailable(string code) = __traits(compiles,
{
     mixin (code);
});

enum hasStaticForeach = hasFeatureAvailable!("static foreach(i; 
0..1) {}");

pragma (msg, "Supports `static foreach`: ", hasStaticForeach);




More information about the Digitalmars-d mailing list