import checkedint.throws : SmartInt; // use IntFlagPolicy.throws char[3] arr = ['a', 'b', 'c']; SmartInt!long n = 1; // On 32-bit, `long` cannot be used directly for array indexing, static if (size_t.sizeof < long.sizeof) static assert(!__traits(compiles, arr[n])); // but idx can be used to concisely and safely cast to ptrdiff_t: assert(arr[n.idx] == 'b'); // The conversion is bounds checked: static if (size_t.sizeof < long.sizeof) { n = long.min; try { arr[n.idx] = '?'; } catch (CheckedIntException e) { assert(e.intFlags == IntFlag.negOver); } }
Convert this value to a type suitable for indexing an array:
checkedint.to() is used for bounds checking.