D Language
コンパイル時間関数評価(CTFE)
サーチ…
備考
CTFEは、コンパイラがコンパイル時に関数を実行できるようにするメカニズムです。この機能を使用するために必要なD言語の特別なセットはありません。コンパイル時にDコンパイラが解釈することを決定した既知の値を関数がコンパイル時に依存している場合は常にそうです。
また、CTFEとインタラクティブにプレイすることもできます。
コンパイル時に関数を評価する
long fib(long n)
{
return n < 2 ? n : fib(n - 1) + fib(n - 2);
}
struct FibStruct(int n) { // Remarks: n is a template
ubyte[fib(n)] data;
}
void main()
{
import std.stdio : writeln;
enum f10 = fib(10); // execute the function at compile-time
pragma(msg, f10); // will print 55 during compile-time
writeln(f10); // print 55 during runtime
pragma(msg, FibStruct!11.sizeof); // The size of the struct is 89
}
Modified text is an extract of the original Stack Overflow Documentation
ライセンスを受けた CC BY-SA 3.0
所属していない Stack Overflow