diff --git a/ddili/src/ders/d.cn/cond_comp.d b/ddili/src/ders/d.cn/cond_comp.d index 24b6041..f71f480 100644 --- a/ddili/src/ders/d.cn/cond_comp.d +++ b/ddili/src/ders/d.cn/cond_comp.d @@ -1,39 +1,39 @@ Ddoc -$(DERS_BOLUMU $(IX conditional compilation) Conditional Compilation) +$(DERS_BOLUMU $(IX 条件编译) 条件编译) $(P -Conditional compilation is for compiling parts of programs in special ways depending on certain compile time conditions. Sometimes, entire sections of a program may need to be taken out and not compiled at all. +条件编译,顾名思义,就是指在编译时根据条件选择最后要编译哪一部分的代码。有时一整段程序都应从源代码中剔除无需编译。 ) $(P -Conditional compilation involves condition checks that are evaluable at compile time. Runtime conditional statements like $(C if), $(C for), $(C while) are not conditional compilation features. +条件编译包将在编译期执行条件检查。像 $(C if)、$(C for)、$(C while) 这种运行期条件检查不属于条件编译的范畴。 ) $(P -We have already encountered some features in the previous chapters, which can be seen as conditional compilation: +我们已经在之前的章节中见到过可被看作是条件编译的特性: ) $(UL $(LI -$(C unittest) blocks are compiled and run only if the $(C -unittest) compiler switch is enabled. +$(C unittest) 代码块只在有编译器开关 $(C -unittest) 打开时才会编译并运行 。 ) $(LI -The contract programming blocks $(C in), $(C out), and $(C invariant) are activated only if the $(C -release) compiler switch is $(I not) enabled. +契约编程中的 $(C in)、$(C out) 和 $(C invariant) 代码块只有在编译器开关 $(C -release) $(I 没有)被打开时才会编译执行。 ) ) $(P -Unit tests and contracts are about program correctness; whether they are included in the program should not change the behavior of the program. +单元测试和契约是用来保证程序的正确性的;它们就算被包含在程序中也不会改变程序的功能。 ) $(UL $(LI -Template specializations are compiled into the program only for specific types. When a specialization is not actually used in the program, the specialization is not compiled: +只有特定类型的模版特化会被编译入程序。如果特化从未在程序中被使用,它将不会被编译: --- void swap(T)(ref T lhs, ref T rhs) { @@ -54,7 +54,7 @@ unittest { void swap(T $(HILITE : uint))(ref T lhs, ref T rhs) { lhs ^= rhs; rhs ^= lhs; - lhs ^= rh; // TYPO! + lhs ^= rh; // 错误的代码! } void main() { @@ -62,15 +62,15 @@ void main() { --- $(P -The $(C uint) specialization above has been implemented by taking advantage of the $(C ^) ($(I xor)) operator, presumably under the belief that it would be executed faster than the general algorithm. ($(I $(B Note:) To the contrary, on most modern microprocessors this method is slower than the one that uses a temporary variable.)) +上面这个针对 $(C uint) 的模版特化利用了 $(C ^)($(I xor,异或))运算符,它貌似比通用算法执行的更快。($(I $(B 注:) 实际正好相反,在大多数现代微处理器上,这种方法比使用临时变量的方法慢。)) ) $(P -Despite the typo at the end of that specialization, since the $(C uint) specialization is never actually used, the program gets compiled without any errors. +虽然特化的最后一行代码出现了拼写错误,但由于 $(C uint) 特化从未被使用,因此程序可以顺利通过编译。 ) $(P -$(I $(B Note:) This is another example of how important unit tests are; the mistake would have been noticed if there were a unit test for that specialization: +$(I $(B 注:) 这也是一个说明单元测试重要性的例子;如果编写了一个针对此特化的单元测试,这个错误将被发现: ) ) @@ -86,7 +86,7 @@ unittest { --- $(P -This example shows that template specializations are also compiled under certain conditions. +这个例子表明在特定的条件下,模版特化的确会被编译。 ) ) @@ -94,58 +94,58 @@ This example shows that template specializations are also compiled under certain ) $(P -The following are the features of D that are specifically for conditional compilation: +下面这些 D 语言特性专门用于条件编译: ) $(UL $(LI $(C debug)) $(LI $(C version)) $(LI $(C static if)) -$(LI $(C is) expression) +$(LI $(C is) 表达式) $(LI $(C __traits)) ) $(P -We will see the $(C is) expression in the next chapter and $(C __traits) in a later chapter. +我们将在下一章学习 $(C is),再下一章学习 $(C __traits)。 ) $(H5 $(IX debug) debug) $(P -$(IX -debug, compiler switch) $(C debug) is useful during program development. The expressions and statements that are marked as $(C debug) are compiled into the program only when the $(C -debug) compiler switch is enabled: +$(IX -debug, 编译器开关) 在程序开发过程中,$(C debug) 非常有用。被标记为 $(C debug) 的表达式和语句只有在编译开关 $(C -debug) 打开时才会被编译进程序: ) --- debug $(I a_conditionally_compiled_expression); debug { - // ... conditionally compiled code ... + // ... 满足条件时编译的代码 ... } else { - // ... code that is compiled otherwise ... + // ... 不满足条件时编译的代码 ... } --- $(P -The $(C else) clause is optional. +$(C else) 子句是可选的。 ) $(P -Both the single expression and the code block above are compiled only when the $(C -debug) compiler switch is enabled. +只有当编译开关 $(C -debug) 打开,那条单独的表达式和上方的代码块才会被编译。 ) $(P -We have been adding statements into the programs, which printed messages like "adding", "subtracting", etc. to the output. Such messages (aka $(I logs) and $(I log) messages) are helpful in finding errors by visualizing the steps that are taken by the program. +我们可以为程序添加一下输出像“adding”,“subtracting”这样的语句。这种信息(又称 $(I logs) 和 $(I log) 信息)可视化了程序的执行流程,帮助我们找到错误。 ) $(P -Remember the $(C binarySearch()) function from $(LINK2 /ders/d.en/templates.html, the Templates chapter). The following version of the function is intentionally incorrect: +还记得 $(LINK2 /ders/d.en/templates.html, 模版) 一章中的 $(C binarySearch()) 函数吗?下面这个版本故意留有错误: ) --- import std.stdio; -// WARNING! This algorithm is wrong +// 警告!此算法是错误的 size_t binarySearch(const int[] values, in int value) { if (values.length == 0) { return size_t.max; @@ -173,7 +173,7 @@ void main() { --- $(P -Although the index of 42 is 6, the program incorrectly reports 1: +虽然 42 的索引是 6,但程序错误输出了 1: ) $(SHELL_SMALL @@ -181,7 +181,7 @@ Index: 1 ) $(P -One way of locating the bug in the program is to insert lines that would print messages to the output: +一种定位 bug 的方法是在程序中添加几行输出信息的代码: ) --- @@ -213,7 +213,7 @@ size_t binarySearch(const int[] values, in int value) { --- $(P -The output of the program now includes steps that the program takes: +现在程序的输出中包含了其执行的每一步操作: ) $(SHELL_SMALL @@ -230,11 +230,11 @@ Index: 1 ) $(P -Let's assume that the previous output does indeed help the programmer locate the bug. It is obvious that the $(C writefln()) expressions are not needed anymore once the bug has been located and fixed. However, removing those lines can also be seen as wasteful, because they might be useful again in the future. +我们假设上面这段输出的确帮助了程序员定位 bug。显然在 bug 已被修复后就这些 $(C writefln()) 表达式就没用了。然而,删除这几行却有点浪费,因为我们将来可能还需要用到它们。 ) $(P -Instead of being removed altogether, the lines can be marked as $(C debug) instead: +可以将其标记为 $(C debug) 而不是移除他们: ) --- @@ -242,17 +242,17 @@ Instead of being removed altogether, the lines can be marked as $(C debug) inste --- $(P -Such lines are included in the program only when the $(C -debug) compiler switch is enabled: +只有在编译器开关 $(C -debug) 打开时这几行才会被编译进程序: ) $(SHELL_SMALL dmd deneme.d -ofdeneme -w $(HILITE -debug) ) -$(H6 $(C debug($(I tag)))) +$(H6 $(C debug($(I 标签)))) $(P -If there are many $(C debug) keywords in the program, possibly in unrelated parts, the output may become too crowded. To avoid that, the $(C debug) statements can be given names (tags) to be included in the program selectively: +如果程序中有许多不相干的 $(C debug) 关键字,输出可能会一片混乱。为了解决这类问题,我们可以为 $(C debug) 语句加上名字(标签),选择那一部分需要添加进程序: ) --- @@ -260,15 +260,16 @@ If there are many $(C debug) keywords in the program, possibly in unrelated part --- $(P -The tagged $(C debug) statements are enabled by the $(C -debug=$(I tag)) compiler switch: +命名的 $(C debug) 语句应使用编译开关 $(C -debug=$(I tag)) 来启用: ) + $(SHELL_SMALL dmd deneme.d -ofdeneme -w $(HILITE -debug=binarySearch) ) $(P -$(C debug) blocks can have tags as well: +也可以为 $(C debug) 代码块指定标签: ) --- @@ -278,7 +279,7 @@ $(C debug) blocks can have tags as well: --- $(P -It is possible to enable more than one $(C debug) tag at a time: +一次性启用多个 $(C debug) 标签是可行的: ) $(SHELL_SMALL @@ -286,13 +287,13 @@ $ dmd deneme.d -w $(HILITE -debug=binarySearch) $(HILITE -debug=stackContainer) ) $(P -In that case both the $(C binarySearch) and the $(C stackContainer) debug statements and blocks would be included. +在上面这种编译条件下,标签为 $(C binarySearch) 和 $(C stackContainer) 的语句和代码块都会被包含进程序。 ) -$(H6 $(C debug($(I level)))) +$(H6 $(C debug($(I 等级)))) $(P -Sometimes it is more useful to associate $(C debug) statements by numerical levels. Increasing levels can provide more detailed information: +有时我们可以把 $(C debug) 语句和数字的等级联系在一起。通过增高等级可获得更详细的信息: ) --- @@ -310,7 +311,7 @@ void myFunction(string fileName, int[] values) { } } - // ... the implementation of the function ... + // ... 函数实现 ... } void main() { @@ -319,7 +320,7 @@ void main() { --- $(P -The $(C debug) expressions and blocks that are lower than or equal to the specified level would be compiled: +低于或等于指定等级的 $(C debug) 表达式或代码块将会被编译: ) $(SHELL_SMALL @@ -329,7 +330,7 @@ $(SHELL_OBSERVED entered myFunction) ) $(P -The following compilation would provide more information: +下面这次编译能提供更多调试信息: ) $(SHELL_SMALL @@ -343,40 +344,40 @@ the arguments: 2: 100) ) -$(H5 $(IX version) $(C version($(I tag))) and $(C version($(I level)))) +$(H5 $(IX version) $(C version($(I 标签))) 和 $(C version($(I 等级)))) $(P -$(C version) is similar to $(C debug) and is used in the same way: +$(C version) 的用途和 $(C debug) 类似: ) --- - version(testRelease) /* ... an expression ... */; + version(testRelease) /* ... 一个表达式 ... */; version(schoolRelease) { - /* ... expressions that are related to the version of - * this program that is presumably shipped to schools ... */ + /* ... 与将要发给学校的 + * 那个版本有关的代码 ... */ } else { - // ... code compiled otherwise ... + // ... 不满足条件时编译的代码 ... } version(1) aVariable = 5; version(2) { - // ... a feature of version 2 ... + // ... 版本 2 包含的特性 ... } --- $(P -The $(C else) clause is optional. +$(C else) 子句是可选的。 ) $(P -Although $(C version) works essentially the same as $(C debug), having separate keywords helps distinguish their unrelated uses. +虽然 $(C version) 实质上与 $(C debug) 作用相同,但这两个关键字有助于区分不同的功能。 ) $(P -$(IX -version, compiler switch) As with $(C debug), more than one $(C version) can be enabled: +$(IX -version, 编译开关) 和 $(C debug) 一样,我们可以一次性启用多个 $(C version): ) $(SHELL_SMALL @@ -384,14 +385,14 @@ $ dmd deneme.d -w $(HILITE -version=record) $(HILITE -version=precise_calculatio ) $(P -There are many predefined $(C version) tags, the complete list of which is available at the $(LINK2 http://dlang.org/version.html, Conditional Compilation specification). The following short list is just a sampling: +D 语言预先定义了许多 $(C version) 标签,完整的标签列表参见 $(LINK2 http://dlang.org/version.html, 条件编译规范)。下面这个简短的表格只是一个其中一部分: ) -
| The compiler | $(B DigitalMars GNU LDC SDC) |
| 编译器 | $(B DigitalMars GNU LDC SDC) |
| The operating system | $(B Windows Win32 Win64 linux OSX Posix FreeBSD + |
| 操作系统 | $(B Windows Win32 Win64 linux OSX Posix FreeBSD OpenBSD NetBSD DragonFlyBSD @@ -404,8 +405,8 @@ SysV3 SysV4 Hurd) |
| CPU endianness | $(B LittleEndian BigEndian) |
| Enabled compiler switches | $(B D_Coverage D_Ddoc D_InlineAsm_X86 D_InlineAsm_X86_64 D_LP64 D_PIC D_X32 + |
| CPU 字节序 | $(B LittleEndian BigEndian) |
| 打开编译开关 | $(B D_Coverage D_Ddoc D_InlineAsm_X86 D_InlineAsm_X86_64 D_LP64 D_PIC D_X32 D_HardFloat D_SoftFloat D_SIMD @@ -415,8 +416,8 @@ unittest assert ) |
| CPU architecture | $(B X86 X86_64) |
| Platform | $(B Android + |
| CPU 架构 | $(B X86 X86_64) |
| 平台 | $(B Android Cygwin MinGW ARM @@ -460,16 +461,16 @@ Alpha_HardFP |