Dlang equivalent of #define/#ifdef : not... version
Steven Schveighoffer
schveiguy at gmail.com
Tue Apr 20 23:58:46 UTC 2021
On 4/20/21 2:57 PM, ichneumwn wrote:
> Hi,
>
> Trying to convert a C header to D. The underlying package exists in
> different versions and may or may not expose certain functionality
> (modules/extensions). One should check at compile time that the header
> provides the relevant definitions, through #ifdef, and do a further
> run-time check to confirm the functionality is really present. It is the
> compile time check that I am finding tricky to do/emulate.
>
> .h : #define i_am_a_feature 1
enum i_am_a_feature = true;
>
> The C-code uses this define as a guard:
>
> .c : #ifdef i_am_a_feature
static if(i_am_a_feature) {
...
}
> So my questions:
> - is there a module-crossing equivalent of "version"?
enum isn't exactly "module crossing", but it's defined within the
module, which means you can look at it from elsewhere.
> - if not, is there some way I could test for the existence of the enum
> can_i_test_for_this? A SymbolExists!() or ModuleHasSymbol!() or
> ModuleHasMember!() ?
This is surprisingly tricky. A template will not accept a symbol by
alias that doesn't exist, and if you pass in a string and test, it will
test if that template can see the symbol, not your code. You can
probably do it inline:
static if(__traits(compiles, {alias x = doesThisExist;}));
-Steve
More information about the Digitalmars-d-learn
mailing list