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

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


#if condition is replaced by static if(condition) (with much more compile time evaluation)

#ifdef ident is replaced by version(ident)

#define ident is replaced by version = ident

#define ident replacement is replaced by alias ident replacement

more information at http://dlang.org/version.html and a list of predefined version defines


This could be of some use regarding preprocessor directives in C vs D: http://dlang.org/pretod.html

Regarding the detection of OS and processor type, this thread looks like it might answer your questions: http://forum.dlang.org/thread/[email protected]?page=1

Note: I am familiar with C/C++ but not D. If my answer is insufficient, let me know so I can change it. Hopefully I've pointed you in the right direction.

Tags:

D

Preprocessor