|
Archives
D Programming
digitalmars.Ddigitalmars.D.bugs digitalmars.D.dtl digitalmars.D.ide digitalmars.D.dwt digitalmars.D.announce digitalmars.D.learn digitalmars.D.debugger D.gnu D C/C++ Programming
c++c++.announce c++.atl c++.beta c++.chat c++.command-line c++.dos c++.dos.16-bits c++.dos.32-bits c++.idde c++.mfc c++.rtl c++.stl c++.stl.hp c++.stl.port c++.stl.sgi c++.stlsoft c++.windows c++.windows.16-bits c++.windows.32-bits c++.wxwindows digitalmars.empire digitalmars.DMDScript electronics |
digitalmars.D.learn - Template limits in D2
I am trying to create a non-dynamic array at compile time, so I have written
this test code:
int sumSqrt(int n) {
int result = 0;
while (n) {
int digit = n % 10;
n /= 10;
result += digit * digit;
}
return result;
}
template GenSquares(int n) {
static if (n < 0)
const int[] GenSquares = [];
else
const int[] GenSquares = GenSquares!(n-1) ~ [sumSqrt(n)];
}
void main() {
const int CHUNK = 1000;
static const auto squares = cast(int[CHUNK])GenSquares!(CHUNK-1);
}
That code works with D1. But with D2 it gives:
templates_test.d(15): Error: template instance templates_test.GenSquares!(499)
recursive expansion
Do you know why D2 allows less templates?
The second problem is that compile-time functions are nicer, so I'd like to not
use templates when possible. But the following code doesn't work at compile
time, can you tell me why? (I have had to use a not nice temporary struct to
return the static array)
struct S(int N) { int[N] a; }
S!(N) genSquares(int N)() {
S!(N) s;
for (int i = 0; i < N; i++) {
int n = i;
int m = 0;
while (n) {
int digit = n % 10;
n /= 10;
m += digit * digit;
}
s.a[i] = m;
}
return s;
}
void main() {
const int CHUNK = 1000;
static const auto squares = genSquares!(CHUNK)().a;
}
Bye and thank you,
bearophile
May 24 2009
bearophile wrote: ...The second problem is that compile-time functions are nicer, so I'd like to not use templates when possible. But the following code doesn't work at compile time, can you tell me why? (I have had to use a May 24 2009
Lutger escribió:bearophile wrote: ...The second problem is that compile-time functions are nicer, so I'd like to not use templates when possible. But the following code doesn't work at compile time, can you tell me why? (I have had to use a May 24 2009
Ary Borenszweig escribió:Lutger escribió:bearophile wrote: ...The second problem is that compile-time functions are nicer, so I'd like to not use templates when possible. But the following code doesn't work at compile time, can you tell me why? (I have had to use a May 24 2009
Hello Ary,BTW, I had to debug inside Descent's code to find this. If I debug it using the debugger I'm programming, I can see it stops the execution right at the "s.a[i] = m;" line, without saying why (DMD doesn't say why). It's not much, but I think it's better than "Can't evaluate at compile-time", and might give you more clues about it. :-) May 24 2009
Ary Borenszweig wrote: ...BTW, I had to debug inside Descent's code to find this. If I debug it using the debugger I'm programming, I can see it stops the execution right at the "s.a[i] = m;" line, without saying why (DMD doesn't say why). It's not much, but I think it's better than "Can't evaluate at compile-time", and might give you more clues about it. :-) May 24 2009
|