Casting ulong to int with @nogc
Selim Ozel
sozel at wpi.edu
Tue Aug 17 06:39:56 UTC 2021
On Friday, 30 July 2021 at 05:20:37 UTC, Rumbu wrote:
> On Wednesday, 28 July 2021 at 17:41:04 UTC, Selim Ozel wrote:
>> import std.conv:to;
>>
>> ```d
>> import std.conv:to;
>>
>> @nogc int
>> myFunction(ulong number){
>> return to!int(number);
>> }
>> ```
>>
>> Since std.conv.to is a non at nogc myFunction throws a compiler
>> error. From a practical perspective length property of arrays
>> return ulong. What do you think is a reasonable way of casting
>> that into int with no-gc.
>>
>> Thanks,
>> Selim
>
> Length property of arrays is architecture dependent returning
> ulong for 64 bit and uint for 32 bit. You can use size_t as
> data type to cover both cases.
>
> Therefore your function can be portable without any conversion:
>
> ```d
> @nogc size_t
> myFunction(size_t number) {
> return number;
> }
> ```
Thank you. I'll try this one as well. The conversion suggested
above worked fine for my purposes but it does look a bit ugly :)
I was not aware of the option to use soze_t.
S
More information about the Digitalmars-d
mailing list