C编程语言中“#ifdef”,“#ifnf”,“#else”,“#elif”,“#define”,“#undef”的类似物是什么?

时间:2014-07-04 21:34:27

标签: preprocessor d

在C / C ++中,我们有预处理器指令(参见问题的标题)。 D语言中它们的类比是什么? 如何在编译时检测操作系统类型(Windows,Linux,Mac OS X,FreeBSD,...)和处理器类型(例如:32或64位)?

3 个答案:

答案 0 :(得分:8)

更新:最佳答案已在dlang.org上发布:http://dlang.org/pretod.html

D没有预处理器。相反,它提供了强大的编译时评估和内省功能。

以下是典型的C / C ++到D翻译的简单列表,其中包含指向相关文档的链接:


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

D version [link]


C / C ++ #if <condition>

D static if [link]


C / C ++ #define

D :D翻译取决于具体情况。

#define FOO这样的简单C / C ++定义被翻译为D“s version”。示例:version = FOO

#define BAR 40之类的代码已转换为以下D代码:enum BAR 40或在极少数情况下您可能需要使用alias

复杂的定义,如#define GT_CONSTRUCT(depth,scheme,size) \ ((depth) | (scheme) | ((size) << GT_SIZE_SHIFT))被翻译成D的模板:

// 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 :我没有足够的翻译

答案 1 :(得分:7)

#if conditionstatic if(condition)替换(编译时评估更多)

#ifdef ident已被version(ident)

取代

#define ident已被version = ident

取代

#define ident replacement已被alias ident replacement

取代

http://dlang.org/version.html的更多信息以及predefined version defines

的列表

答案 2 :(得分:3)

这可能对C与D中的预处理程序指令有一些用处: http://dlang.org/pretod.html

关于操作系统和处理器类型的检测,该线程看起来可能会回答您的问题:http://forum.dlang.org/thread/mailman.616.1387191250.3242.digitalmars-d-learn@puremagic.com?page=1

注意:我熟悉C / C ++但不熟悉D.如果我的答案不够,请告诉我,以便我可以更改。希望我能指出你正确的方向。

相关问题