digitalmars.D.learn - Array length : size_t
- Namespace (7/7) Sep 18 2013 D's Array length is currently of type size_t, which means on 32
- Dicebot (20/27) Sep 18 2013 Perfectly safe way is to do run-time range check followed by a
- H. S. Teoh (9/16) Sep 18 2013 If the C function only accepts int, then just use to!int(array.size). If
- Dicebot (3/12) Sep 18 2013 Ah, `to!()` does check valid ranges before conversion? Good to
- H. S. Teoh (13/22) Sep 18 2013 Yes, it's handled explicitly by the following overload of toImpl() in
- Namespace (3/25) Sep 19 2013 So to!int is safer but slower and a cast would be unsafe but
- bearophile (4/6) Sep 19 2013 Right.
- Namespace (2/8) Sep 19 2013 Thanks!
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.
Sep 18 2013
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)); }
Sep 18 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
Sep 18 2013
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.
Sep 18 2013
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: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.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.
Sep 18 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:So to!int is safer but slower and a cast would be unsafe but faster?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
Sep 19 2013
Namespace:So to!int is safer but slower and a cast would be unsafe but faster?Right. Bye, bearophile
Sep 19 2013
On Thursday, 19 September 2013 at 11:10:08 UTC, bearophile wrote:Namespace:Thanks!So to!int is safer but slower and a cast would be unsafe but faster?Right. Bye, bearophile
Sep 19 2013