import std.string;
void func(A, B)(A a, B b) {
pragma(msg, format("用'%s'和'%s'调用",A.stringof, B.stringof));
// ...
}
void main() {
func(42, 1.5);
func("你好", 'a');
}
编译时生成消息.运行时没有.
pragma(lib,"curl");
指示与哪个库连接
pragma(inline)
指定是否内联.
import std.stdio;
import std.datetime;//StopWatch
ubyte compute(ubyte i) {
return cast(ubyte)(i * 42);
}
void main() {
size_t counter = 0;
StopWatch sw;
sw.start();
foreach (i; 0 .. 10000_0000) {//1亿次
const number = cast(ubyte)i;
if (compute(number) == number) {
++counter;
}
//const r= cast(ubyte)(number * 42);
//if(r == number) {//等价内联
}
sw.stop();
writefln("%s毫秒", sw.peek.msecs);
}
命令行$ dmd deneme.d -w -inline
调用函数内联.缺点是,有时代码体太多太膨胀.
pragma(inline, false):
,即使编译器有内联选项
也不内联.
pragma(inline, true)
,一定内联,编译器无法内联时会报错.
pragma(inline)
,由编译器选项-inline
决定.
pragma(inline, false) {
// 不应内联
// ...
}
int foo() {
pragma(inline, true); //应内联
// ...
}
pragma(inline, true):
// 应该内联
// ...
pragma(inline):
// 由编译器`-inline`决定
// ...
pragma(startaddress)
指示程序起始地址.
pragma(mangle)
指示混杂符号
//这样用
pragma(mangle, "body")
extern(C) string c_body(string);
void main() {
auto s = c_body("hello");//真实名字为`body`
}
当然进行c++等的链接时也很有用.