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]
    @property
    Select!(isSigned!N, ptrdiff_t, size_t)
    idx
    @safe
    (
    N
    )
    (
    const N num
    )
    if (
    isScalarType!N ||
    isCheckedInt!N
    )
  2. Select!(isSigned!(BasicScalar!N), ptrdiff_t, size_t) idx [@property getter]

Examples

1 import checkedint.sticky : idx, SmartInt, safeInt; // use IntFlagPolicy.sticky
2 
3 assert(is(typeof(idx(cast(long)1)) == ptrdiff_t));
4 assert(is(typeof(idx(cast(ubyte)1)) == size_t));
5 assert(is(typeof(idx(SmartInt!ulong(1))) == size_t));
6 
7 assert(idx(17uL) == 17);
8 assert(idx(-3) == -3);
9 assert(idx(safeInt(cast(byte)100)) == 100);
10 
11 static if (size_t.sizeof == 4)
12 {
13     idx(ulong.max);
14     assert(IntFlags.local.clear() == IntFlag.posOver);
15 
16     idx(long.min);
17     assert(IntFlags.local.clear() == IntFlag.negOver);
18 }
1 import checkedint.throws : SmartInt, SafeInt; // use IntFlagPolicy.throws
2 
3 assert( isSafeInt!(SafeInt!int));
4 
5 assert(!isSafeInt!int);
6 assert(!isSafeInt!(SmartInt!int));
1 import checkedint.throws : SmartInt, SafeInt; // use IntFlagPolicy.throws
2 
3 assert( isSmartInt!(SmartInt!int));
4 
5 assert(!isSmartInt!int);
6 assert(!isSmartInt!(SafeInt!int));
1 import checkedint.throws : SmartInt, SafeInt; // use IntFlagPolicy.throws
2 
3 assert( isCheckedInt!(SafeInt!int));
4 assert( isCheckedInt!(SmartInt!int));
5 
6 assert(!isCheckedInt!int);

Meta