例のごとくコンパイルエラーで結果出力ですが、なるべくすなおーに書いてみました。 D のコンパイル時プログラミングはなかなか悪くないとわかるのではないでしょうか。
template g(int n) {
const int result = n;
static if (n == 1) {
const int step = 1;
}
else static if (n & 1) {
const int step = g!(3*n+1).step+1;
}
else {
const int step = g!(n/2).step+1;
}
}
template h(int m) {
static if (m == 1) {
const int step = 1;
const int result = 1;
}
else {
alias g!(m) head;
alias h!(m-1) tail;
static if (head.step > tail.step) {
alias head max;
}
else {
alias tail max;
}
const int step = max.step;
const int result = max.result;
}
}
static assert(false, h!(100).result);