Array length : size_t

H. S. Teoh hsteoh at quickfur.ath.cx
Wed Sep 18 15:19:26 PDT 2013


On Wed, Sep 18, 2013 at 10:46:18PM +0200, Namespace wrote:
> D's Array length is currently of type size_t, which means on 32 bit
> it's an uint and on 64 bit an ulong. This is difficult: What if I
> want to give the length of an array as parameter to some C functions
> which accepts only an int?
> What is the right/safe way to do this? A cast? Or is there something
> better?
> I like to avoid cast's if they aren't necessary.

If the C function only accepts int, then just use to!int(array.size). If
the size overflows int, to() will throw an exception which you can
handle. This is probably the best you can do anyway, since if the C
function doesn't take anything bigger than int, then there's no way you
can pass the real size to it.


T

-- 
PNP = Plug 'N' Pray


More information about the Digitalmars-d-learn mailing list