Thread overview | |||||||
---|---|---|---|---|---|---|---|
|
June 25, 2014 Using two flags in conditonal compilation (version) | ||||
---|---|---|---|---|
| ||||
Hi, In the development of my library, I'm in a position where I need to add support for multiple compilers. For instance, supporting both the assembly of LDC/DMD and GDC. I want to do something like: version(DigitalMars && LDC) { } However, it doesn't compile which forces me to rewrote the same code for both DigitalMars and LDC version(DigitalMars) { } version(LDC) { } Is there a way to check both versions at the same time? (I can't seem to find the solution through google, sorry) Thanks, Danyal Zia |
June 25, 2014 Re: Using two flags in conditonal compilation (version) | ||||
---|---|---|---|---|
| ||||
Posted in reply to Danyal Zia | Danyal Zia:
> Is there a way to check both versions at the same time? (I can't seem to find the solution through google, sorry)
This is close to being the best solution in D (untested):
version(DigitalMars) enum myMars = true; else enum myMars = false;
version(LDC) enum myLdc = true; else enum myLdc = false;
enum myMarsOrLdc = myMars || myLdc;
static if (myMarsOrLdc) {
...
} else {
...
}
Bye,
bearophile
|
June 25, 2014 Re: Using two flags in conditonal compilation (version) | ||||
---|---|---|---|---|
| ||||
Posted in reply to Danyal Zia | On Wed, 25 Jun 2014 20:24:30 +0000, Danyal Zia wrote:
> Hi, In the development of my library, I'm in a position where I need to add support for multiple compilers. For instance, supporting both the assembly of LDC/DMD and GDC. I want to do something like:
>
> version(DigitalMars && LDC)
> {
> }
>
> However, it doesn't compile which forces me to rewrote the same code for both DigitalMars and LDC
>
> version(DigitalMars)
> {
> }
>
> version(LDC)
> {
> }
>
> Is there a way to check both versions at the same time? (I can't seem to find the solution through google, sorry)
>
> Thanks,
> Danyal Zia
I think you mean ||, not &&. The best way I know around this is to define enums:
version (DigitalMars)
enum compiler_DigitalMars = true;
else
enum compiler_DigitalMars = false;
//... similar for LDC
static if (compiler_DigitalMars || compiler_LDC)
{
...
} else {
...
}
|
June 25, 2014 Re: Using two flags in conditonal compilation (version) | ||||
---|---|---|---|---|
| ||||
Posted in reply to Justin Whear | On Wednesday, 25 June 2014 at 20:30:28 UTC, Justin Whear wrote:
> I think you mean ||, not &&. The best way I know around this is to
> define enums:
>
> version (DigitalMars)
> enum compiler_DigitalMars = true;
> else
> enum compiler_DigitalMars = false;
>
> //... similar for LDC
>
> static if (compiler_DigitalMars || compiler_LDC)
> {
> ...
> } else {
> ...
> }
Yeah, I mean ||. Your solution works, thanks a lot!
|
June 26, 2014 Re: Using two flags in conditonal compilation (version) | ||||
---|---|---|---|---|
| ||||
Posted in reply to Danyal Zia | version(DigitalMars) version = DMDAsm; version(LDC) version = DMDAsm; version(DMDAsm) asm { //dmd/ldc asm here } version(GDC) asm { //gdc asm here } http://dlang.org/version.html#VersionSpecification |
Copyright © 1999-2021 by the D Language Foundation