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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With