OR in version conditional compilation
jxel
jxel at gmall.com
Fri Mar 20 22:33:58 UTC 2020
On Friday, 20 March 2020 at 21:03:55 UTC, Jonathan M Davis wrote:
> On Wednesday, March 18, 2020 10:23:26 AM MDT IGotD- via
> Digitalmars-d-learn wrote:
>> I have not seen any example where version has several OR
>> matches.
>>
>> Example idiom:
>>
>> version(X86_64 || X86)
>> {
>>
>> }
>> else version(ARM || Thumb)
>> {
>>
>> }...
>>
>> you get the idea. So is this possible at all or do you have to
>> duplicate the code for each version identifier despite they
>> are equal for many version identifiers?
>
> To add to what the others have said, the reasons that Walter is
> against having boolean conditions in version statements have to
> do with how using boolean conditions in #ifdefs in C/C++ has
> historically been a big source of bugs. So, disallowing it
> helps prevent certain classes of bugs. It's why druntime
> duplicates most C declarations across platforms.
What kind of bugs are those?
> In general, I think that Walter's approach here is probably the
> right one, but it can certainly be annoying in cases where you
> have to duplicate code that actually is the same rather than
> being subtly different, and a lot of people dislike it enough
> that they use workarounds like those discussed in this thread.
Sadly it still shares probably the worst bug with the C preposser
in that anything that isnt defined simply evaluates to false. If
you have a typo then the code will still "work". His approach
only works if you can put an assert in the else branch. Which is
the case for C/C++ as well. That isnt always possible, as a
result you still have the same problem with typos.
> And of course, some of the same bugs that come up with #ifdefs
> come up with static ifs in generic code (particularly with
> range-based code that changes what it's doing based on arange's
> type), but at least those can be found with thorough tests on a
> single platform, whereas version-related bugs tend to require
> that you run thorough tests on each platform.
>
> - Jonathan M Davis
You still need to test your code on those platforms, so if you
are supporting them you still have related problems.
More information about the Digitalmars-d-learn
mailing list