Thread overview | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
September 18, 2013 Array length : size_t | ||||
---|---|---|---|---|
| ||||
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. |
September 18, 2013 Re: Array length : size_t | ||||
---|---|---|---|---|
| ||||
Posted in reply to Namespace | On Wednesday, 18 September 2013 at 20:46:21 UTC, 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.
Perfectly safe way is to do run-time range check followed by a cast. Maybe using a wrapper:
auto shrinkTo(Target, Source)(Source source)
if (is(Target : Source)) // is implicitly convertable other way around
{
assert(source <= Target.max);
static if (is(typeof(Taget.min)))
assert(source >= Target.min);
return cast(Target)source;
}
void main()
{
import std.stdio;
long a = 50;
long b = int.max; b++;
writeln(shrinkTo!int(a));
writeln(shrinkTo!int(b));
}
|
September 18, 2013 Re: Array length : size_t | ||||
---|---|---|---|---|
| ||||
Posted in reply to Namespace | 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 |
September 18, 2013 Re: Array length : size_t | ||||
---|---|---|---|---|
| ||||
Posted in reply to H. S. Teoh | On Wednesday, 18 September 2013 at 22:20:45 UTC, H. S. Teoh wrote:
> 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.
Ah, `to!()` does check valid ranges before conversion? Good to know, thanks.
|
September 18, 2013 Re: Array length : size_t | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dicebot | On Thu, Sep 19, 2013 at 12:52:55AM +0200, Dicebot wrote: > On Wednesday, 18 September 2013 at 22:20:45 UTC, H. S. Teoh wrote: > >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. > > Ah, `to!()` does check valid ranges before conversion? Good to know, > thanks. Yes, it's handled explicitly by the following overload of toImpl() in std.conv: T toImpl(T, S)(S value) if (!isImplicitlyConvertible!(S, T) && (isNumeric!S || isSomeChar!S || isBoolean!S) && (isNumeric!T || isSomeChar!T || isBoolean!T) && !is(T == enum)) { ... } T -- Too many people have open minds but closed eyes. |
September 19, 2013 Re: Array length : size_t | ||||
---|---|---|---|---|
| ||||
Posted in reply to H. S. Teoh | 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?
|
September 19, 2013 Re: Array length : size_t | ||||
---|---|---|---|---|
| ||||
Posted in reply to Namespace | Namespace:
> So to!int is safer but slower and a cast would be unsafe but faster?
Right.
Bye,
bearophile
|
September 19, 2013 Re: Array length : size_t | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | On Thursday, 19 September 2013 at 11:10:08 UTC, bearophile wrote:
> Namespace:
>
>> So to!int is safer but slower and a cast would be unsafe but faster?
>
> Right.
>
> Bye,
> bearophile
Thanks!
|
Copyright © 1999-2021 by the D Language Foundation