Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

preprocessor

d

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?

like image 926
user1742529 Avatar asked Jul 04 '14 21:07

user1742529


2 Answers

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

like image 125
DejanLekic Avatar answered Nov 18 '22 00:11

DejanLekic


#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

like image 39
ratchet freak Avatar answered Nov 17 '22 23:11

ratchet freak