How to check for combinations of versions
Paul Backus
snarwin at gmail.com
Wed May 5 15:25:31 UTC 2021
On Wednesday, 5 May 2021 at 15:03:16 UTC, Blatnik wrote:
> Currently I resort to something like this, but I'm curious if
> there's a nicer way to do it.
>
> ```D
> version (Version_A) {
> enum Cool_Feature_Supported = true;
> } else version (Version_B) {
> enum Cool_Feature_Supported = true;
> } else {
> enum Cool_Feature_Supported = false;
> }
> ```
This is the officially-recommended way to do it. D's `version`
system is deliberately restricted, in order to avoid the
"`#ifdef` hell" that often plagues C and C++ projects.
However, if you really want something more expressive, and are
confident in your ability to use the extra power responsibly, it
is possible to work around these limitations:
template hasVersion(string identifier) {
mixin(
"version(", identifier, ") enum hasVersion = true;",
"else enum hasVersion = false;"
);
}
// Usage
static if (hasVersion!"Version_A" || hasVersion!"Version_B") {
enum Cool_Feature_Supported = true;
} else {
enum Cool_Feature_Supported = false;
}
More information about the Digitalmars-d-learn
mailing list