知识若不分享 实在没有意义 http://www.d-programming-language-china.org 20070425
点击下面网址查看原文:
http://www.d-programming-language-china.org
by: uFramer D语言论坛 http://www.d-programming-language-china.org
from: http://www.digitalmars.com/d/version.html
version: 基于D 1.013 (Apr 19, 2007)
条件编译是关于编译哪些代码和不编译哪些代码的处理。(在C和C++中,用预处理指令#if / #else #endif实现条件编译)
ConditionalDeclaration:
Condition DeclarationBlock
Condition DeclarationBlock else DeclarationBlock
Condition : Declarations
DeclarationBlock:
Declaration
{ Declarations }
{ }
Declarations:
Declaration
Declaration Declarations
ConditionalStatement:
Condition NoScopeNonEmptyStatement
Condition NoScopeNonEmptyStatement else NoScopeNonEmptyStatement
如果满足条件就编译尾随的声明语句块或语句,否则编译else后面的语句块或语句。
如果是错误的语句,就不编译语句块或语句。
No new scope is introduced, even if the DeclarationBlock or Statement is enclosed by { }.
条件声明和条件语句能嵌套。
The StaticAssert can be used to issue errors at compilation time for branches of the conditional compilation that are errors. Condition comes in the following forms:
Condition:
VersionCondition
DebugCondition
StaticIfCondition
Version Condition版本条件
Versions enable multiple versions of a module to be implemented with a single source file.
VersionCondition:
version ( Integer )
version ( Identifier )
如果整数等于或大于当前版本级别,或者标识符匹配版本标识符就是满足条件。
版本级别或标本标识符可以用命令行开关-version,或者模块内的版本指定,或者编译或预定义设置。
版本标识符有自己独立的名字空间,不能和调试标识符或者模块内的符号相冲突。模块内定义的版本标本标识符不能影响其他导入模块。( 本文出处: http://www.d-programming-language-china.org )
int k;
version (Demo) // compile in this code block for the demo version
{ int i;
int k; // error, k already defined
i = 3;
}
x = i; // uses the i declared above
version (X86)
{
... // implement custom inline assembler version
}
else
{
... // use default, but slow, version
}
1 Version Specification版本指定
VersionSpecification
version = Identifier ;
version = Integer ;
版本指定可以轻易为一个主版本设置功能组,例如:
version (ProfessionalEdition)
{
version = FeatureA;
version = FeatureB;
version = FeatureC;
}
version (HomeEdition)
{
version = FeatureA;
}
...
version (FeatureB)
{
... implement Feature B ...
}
版本标识符或级别不能前向引用:
version (Foo)
{
int x;
}
version = Foo; // error, Foo already used
虽然调试和版本条件表面上看起来有些相像,但他们的目标完全不同。调试语句所加的测试代码在发行版里会被删除。版本语句可以十分方便地提供多个发行版。
这是一个全功能版和演示版的示例:
class Foo
{
int a, b;
version(full)
{
int extrafunctionality()
{
...
return 1; // extra functionality is supported
}
}
else // demo
{
int extrafunctionality()
{
return 0; // extra functionality is not supported
}
}
}
Various different version builds can be built with a parameter to version:
version(n) // add in version code if version level is >= n
{
... version code ...
}
version(identifier) // add in version code if version
// keyword is identifier
{
... version code ...
}
可想而知,可以用命令行 -version=n 和 -version=标识符指定版本。( 本文出处: http://www.d-programming-language-china.org )
2 Predefined Versions预定义版本
为了提高用法的一致性,D 预定义了几个版本标志符和标志符名字空间。版本标志符不会同代码中的其他标志符冲突,它们位于一个独立的名字空间中。预定义的版本标志符是全局的,也就是,它们可用于所有编译的和导入的模块。
1.1 DigitalMars
Digital Mars 是编译器提供商
1.2 X86
Intel 和 AMD 的 32 bit 处理器
1.3 X86_64
AMD 和Intel 的64 bit 处理器
1.4 Windows
Microsoft Windows 系统( 本文出处: http://www.d-programming-language-china.org )
1.5 Win32
Microsoft 32 bit Windows 系统
1.6 Win64
Microsoft 64 bit Windows 系统
1.7 linux
所有的 linux 系统
1.8 LittleEndian
字节序,低位优先
1.9 BigEndian
字节序,高位优先( 本文出处: http://www.d-programming-language-china.org )
2.0 D_Coverage
Coverage analyser is implemented and the -cov switch is thrown
2.1 D_InlineAsm_X86
实现了X86 内联汇编
2.2 none
未定义;用来禁用某段代码
2.3 all
Always defined; used as the opposite of none
如果出现了新的实现,也许会加入新的有意义的版本标志符。
D 语言无可避免地会随着时间演变。因此,以“D_”打头的版本标志符构成的名字空间被保留作为 D 语言规范或者新特征的标志的名字空间。( 本文出处: http://www.d-programming-language-china.org )
Furthermore, predefined version identifiers from this list cannot be set from the command line or from version statements. (This prevents things like both Windows and linux being simultaneously set.)
特定于编译器提供商的版本号可以是预定义的,形式是在提供商商标的标志符之后加上版本号,如:
version(DigitalMars_funky_extension) // DigitalMars 的有趣扩展
{
...
}
一定要把正确的版本号用于正确的目的。例如,在使用特定于提供商的特征时使用该提供商的标识符;在使用特定于操作系统的特征时使用该操作系统的标志符,等等。
Debug Condition调试条件
通常开发人员会构建两种程序,一个发行版,一个调试版。调试版通常包括额外的错误检测代码、测试套件、友好的输出代码等。调试语句的语句体采用条件编译。这是 D中的 #ifdef DEBUG / #endif 。
DebugCondition:
debug
debug ( Integer )
debug ( Identifier )
如果编译时指定了 -debug 选项,就会将调试语句编译进可执行文件内。
如果调试级别>=Integer,就是满足debug (Integer)
如果编译时指定的 -debug(标志符) 选项中的标志符同 标志符 匹配,就会将 debug(标志符) 语句编译进可执行文件内。( 本文出处: http://www.d-programming-language-china.org )
class Foo
{
int a, b;
debug:
int flag;
}
1 Debug Specification调试指定
DebugSpecification
debug = Identifier ;
debug = Integer ;
Debug identifiers and levels are set either by the command line switch -debug or by a DebugSpecification.
Debug specifications only affect the module they appear in, they do not affect any imported modules. Debug identifiers are in their own namespace, independent from version identifiers and other symbols.
It is illegal to forward reference a debug specification:
debug (foo) printf("Foo\n");
debug = foo; // error, foo used before set
Various different debug builds can be built with a parameter to debug:
debug(Integer) { } // add in debug code if debug level is >= Integer
debug(identifier) { } // add in debug code if debug keyword is identifier
These are presumably set by the command line as -debug=n and -debug=identifier.
Static If Condition
StaticIfCondition:
static if ( AssignExpression )
AssignExpression is implicitly converted to a boolean type, and is evaluated at compile time. The condition is satisfied if it evaluates to true. It is not satisfied if it evaluates to false.
It is an error if AssignExpression cannot be implicitly converted to a boolean type or if it cannot be evaluated at compile time.( 本文出处: http://www.d-programming-language-china.org )
StaticIfConditions can appear in module, class, template, struct, union, or function scope. In function scope, the symbols referred to in the AssignExpression can be any that can normally be referenced by an expression at that point.
const int i = 3;
int j = 4;
static if (i == 3) // ok, at module scope
int x;
class C
{ const int k = 5;
static if (i == 3) // ok
int x;
else
long x;
static if (j == 3) // error, j is not a constant
int y;
static if (k == 5) // ok, k is in current scope
int z;
}
template INT(int i)
{
static if (i == 32)
alias int INT;
else static if (i == 16)
alias short INT;
else
static assert(0); // not supported
}
INT!(32) a; // a is an int
INT!(16) b; // b is a short
INT!(17) c; // error, static assert trips
A StaticIfConditional condition differs from an IfStatement in the following ways:
It can be used to conditionally compile declarations, not just statements.
It does not introduce a new scope even if { } are used for conditionally compiled statements.
For unsatisfied conditions, the conditionally compiled code need only be syntactically correct. It does not have to be semantically correct.
It must be evaluatable at compile time.
Static Assert 静态断言:
StaticAssert:
static assert ( AssignExpression );
static assert ( AssignExpression , AssignExpression );
表达式 会在编译时计算,并转换为布尔值。如果为真,就忽略静态断言。如果为假,编译会失败,并给出错误诊断。
与 断言表达式 不同,静态断言 总会由编译器检查计算,除非它们出现在一个未被指定的调试或者版本语句中。
void foo()
{
if (0)
{
assert(0); // never trips
static assert(0); // always trips
}
version (BAR)
{
}
else
{
static assert(0); // trips when version BAR is not defined
}
}
StaticAssert is useful tool for drawing attention to conditional configurations not supported in the code.( 本文出处: http://www.d-programming-language-china.org )
The optional second AssignExpression can be used to supply additional information, such as a text string, that will be printed out along with the error diagnostic.
( lastupdate:20070426 最新文章请访问http://www.d-programming-language-china.org )关于一大步成功社区:
yidabu提倡在交流中学习,在分享中提高
收集感兴趣的知识,写下心得,通过网络与别人一起分享
理解一点就实践一步,收获什么就分享什么,成功就是这样一点点一步步累积起来的
网络只是一个工具,只有自己身心提高才是实实在在的。d-programming-language-china.org为大家提供一个学习交流各种知识的平台