Request assistance converting C's #ifndef to D

tsbockman via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu May 12 23:23:57 PDT 2016


On Friday, 13 May 2016 at 06:05:14 UTC, Andrew Edwards wrote:
> Additionally, what's the best way to handle nested #ifdef's? 
> Those that appear inside structs, functions and the like... I 
> know that global #ifdef's are turned to version blocks but 
> versions blocks cannot be used inside classes, stucts, 
> functions, etc.

`static if` and `version()` can be nested, and both work just 
fine inside classes, structs, functions, etc.:

module app;

version = withPrint;

struct A {
   version(withPrint) {
     class B {
       static if(size_t.sizeof == 4) {
         static void print() {
           import std.stdio : writeln;
           version(unittest) {
             writeln("Hello, 32-bit world of unit tests!");
           } else {
             writeln("Hello, 32-bit world!");
           }
         }
       } else {
         static void print() {
           import std.stdio : writeln;
           version(unittest) {
             writeln("Hello, presumably 64-bit world of unit 
tests!");
           } else {
             writeln("Hello, presumably 64-bit world!");
           }
         }
       }
     }
   }
}

void main() {
   A.B.print();
}

(Try it on DPaste: https://dpaste.dzfl.pl/0fafe316f739)


More information about the Digitalmars-d-learn mailing list