int or size_t ?

2011-05-07 Thread %u
In Patterns of Human Error, the slide 31 point that you should replce int with size_t why that consider an error ?

Re: int or size_t ?

2011-05-07 Thread bearophile
%u: In Patterns of Human Error, the slide 31 point that you should replce int with size_t why that consider an error ? If T is a byte and the array size is 5 billion items, on 64 bit systems...? In the little find() function you compare it with the length, that's a size_t. Someone else will

Re: int or size_t ?

2011-05-07 Thread Andrej Mitrovic
void main() { size_t val = int.max+1; int val2 = val; writeln(val2); } writes -2147483648 That should give you a hint.

Re: int or size_t ?

2011-05-07 Thread %u
size_t val1 = int.max+1; int val2 = int.max+1; writeln(val1); // 2147483648 writeln(val2); // -2147483648 very clear example thanks you both

Re: int or size_t ?

2011-05-07 Thread Andrej Mitrovic
Actually my example was bad. What I wanted to say is that size_t will be 64bit on 64bit platforms while int will stay 32bit. Another difference is that size_t is unsigned. So it's bad to use int even if you're sure you're only going to compile only on 32bit platforms. Here's the relevant

Re: int or size_t ?

2011-05-07 Thread Andrej Mitrovic
Edit: I just saw you've already figured this out. :)

Re: Cannot interpret struct at compile time

2011-05-07 Thread Robert Clipsham
On 07/05/2011 23:36, Andrej Mitrovic wrote: Not too sure, CTFE is a pain in the ass sometimes. What exactly are you trying to do, print field names in a custom way? No, I have a struct that I don't have access to in the scope I'm in, I do however have its type - by using the above, I can

Re: Cannot interpret struct at compile time

2011-05-07 Thread Andrej Mitrovic
One simplistic solution is to use alias this to simulate the same type: struct Foo { int x, y; } string structClone(T)() { return struct ~ T.stringof ~ _ { ~ T.stringof ~ _inner; alias _inner this; this(T...)(T t) { _inner = typeof(_inner)(t); } };; } void

Re: Cannot interpret struct at compile time

2011-05-07 Thread Robert Clipsham
On 08/05/2011 00:39, Andrej Mitrovic wrote: One simplistic solution is to use alias this to simulate the same type: struct Foo { int x, y; } string structClone(T)() { return struct ~ T.stringof ~ _ { ~ T.stringof ~ _inner; alias _inner this; this(T...)(T

Shouldn't duplicate functions be caught by DMD?

2011-05-07 Thread Andrej Mitrovic
I'm not talking about function overloading, but functions with the same parameters inside the same class definition: class Foo { int foo(int i) { return 1; } int foo(int i) { return 1; } void bar() { foo(1); } } void

Getting equivalent elements in a range/array

2011-05-07 Thread Andrej M.
I want to turn this: auto arr = [1, 1, 2, 3, 4, 4]; into this: auto arr2 = [[1, 1], [2], [3], [4, 4]]; I want an array of arrays of the same elements. Lazy or not, I don't care. I thought I could get away with this inside some while loop: auto equals = array(filter!a == b(arr)); arr =