Array length : size_t

Namespace rswhite4 at googlemail.com
Thu Sep 19 04:05:25 PDT 2013


On Wednesday, 18 September 2013 at 22:20:45 UTC, H. S. Teoh wrote:
> 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
So to!int is safer but slower and a cast would be unsafe but 
faster?


More information about the Digitalmars-d-learn mailing list