Search code examples
preprocessord

What are analogs of "#ifdef", "#ifndef", "#else", "#elif", "#define", "#undef" in D programming language?


In C/C++ we have preprocessor directives (see title of the question). What is the analog of them in D language? And how to detect operating system type (Windows, Linux, Mac OS X, FreeBSD, ...) and processor type (e.g.: 32 or 64 bits) at compile-time?


Solution

  • Update: The best answer is already on dlang.org: http://dlang.org/pretod.html .

    D has no preprocessor. Instead it gives powerful compile-time evaluation and introspection capabilities.

    Here is a simple list of typical C/C++ to D translations, with links to relevant documents:


    C/C++: #ifdef, #ifndef, #else, #elif

    D: version [link]


    C/C++: #if <condition>

    D: static if [link]


    C/C++: #define

    D: D translation depends on the case.

    Simple C/C++ define like #define FOO is translated to D's "version". Example: version = FOO

    Code like #define BAR 40 is translated to the following D code: enum BAR 40 or in rare cases you may need to use the alias.

    Complex defines like #define GT_CONSTRUCT(depth,scheme,size) \ ((depth) | (scheme) | ((size) << GT_SIZE_SHIFT)) are translated into D's templates:

    // Template that constructs a graphtype
    template GT_CONSTRUCT(uint depth, uint scheme, uint size) {
      // notice the name of the const is the same as that of the template
      const uint GT_CONSTRUCT = (depth | scheme | (size << GT_SIZE_SHIFT));
    }
    

    (Example taken from the D wiki)


    C/C++: #undef

    D: There is no adequate translation that I know of