idx

Cast num to a basic type suitable for indexing an array.

For signed types, ptrdiff_t is returned. For unsigned types, size_t is returned.

  1. Select!(isSigned!N, ptrdiff_t, size_t) idx [@property getter]
  2. Select!(isSigned!(BasicScalar!N), ptrdiff_t, size_t) idx [@property getter]
    @property @safe
    Select!(isSigned!(BasicScalar!N), ptrdiff_t, size_t)
    idx
    (
    N
    )
    (
    const N num
    )

Examples

import checkedint.sticky : idx, SmartInt, safeInt; // use IntFlagPolicy.sticky

assert(is(typeof(idx(cast(long)1)) == ptrdiff_t));
assert(is(typeof(idx(cast(ubyte)1)) == size_t));
assert(is(typeof(idx(SmartInt!ulong(1))) == size_t));

assert(idx(17uL) == 17);
assert(idx(-3) == -3);
assert(idx(safeInt(cast(byte)100)) == 100);

static if (size_t.sizeof == 4)
{
    idx(ulong.max);
    assert(IntFlags.local.clear() == IntFlag.posOver);

    idx(long.min);
    assert(IntFlags.local.clear() == IntFlag.negOver);
}

Meta