SafeInt.opAssign

Assign the value of that to this SafeInt instance.

Trying to assign a value that cannot be proven at compile time to be representable by N is an error. Use checkedint.to() to safely convert that with runtime bounds checking, instead.

  1. this(M that)
  2. typeof(this) opAssign(M that)
    struct SafeInt(N, IntFlagPolicy _policy, Flag!"bitOps" bitOps = Yes.bitOps)
    ref return pure @safe nothrow @nogc
    typeof(this)
    opAssign
    (
    M
    )
    (
    const M that
    )
    if (
    isScalarType!M
    )
    if (
    isIntegral!N &&
    )

Examples

import checkedint.sticky : SafeInt, to; // use IntFlagPolicy.sticky

// Only types that for which N can represent all values are accepted directly:
SafeInt!int n = int.max;
n = byte.max;
n = wchar.max;

// Values of a type that could be `< N.min` or `> N.max` are rejected at compile time:
static assert(!__traits(compiles, n = long.max));
static assert(!__traits(compiles, n = uint.max));
static assert(!__traits(compiles, n = real.max));

// Instead, use checkedint.to(), which does runtime bounds checking:
n = to!int(315L);
assert(n == 315);

n = to!int(long.max);
assert(IntFlags.local.clear() == IntFlag.posOver);

Meta