mir_slice.toConst

Cast to const and immutable slices in case of underlying range is a pointer.

  1. auto toImmutable()
  2. auto toConst()
    struct mir_slice(Iterator_, size_t N_ = 1, SliceKind kind_ = Contiguous, Labels_...)
    scope return const @trusted pure nothrow @nogc
    @optmath
    static if(allSatisfy!(isPointer, Iterator, Labels))
    toConst
    ()
    ()
    if (
    0 < N_ &&
    N_ < 255
    &&
    !(
    kind_ == Canonical &&
    N_ == 1
    )
    &&
    Labels_.length <= N_
    &&
    isIterator!Iterator_
    )

Examples

static struct Foo
{
    Slice!(int*) bar;

    int get(size_t i) immutable
    {
        return bar[i];
    }

    int get(size_t i) const
    {
        return bar[i];
    }

    int get(size_t i) inout
    {
        return bar[i];
    }
}
Slice!(double*, 2, Universal) nn;
Slice!(immutable(double)*, 2, Universal) ni;
Slice!(const(double)*, 2, Universal) nc;

const Slice!(double*, 2, Universal) cn;
const Slice!(immutable(double)*, 2, Universal) ci;
const Slice!(const(double)*, 2, Universal) cc;

immutable Slice!(double*, 2, Universal) in_;
immutable Slice!(immutable(double)*, 2, Universal) ii;
immutable Slice!(const(double)*, 2, Universal) ic;

nc = nc; nc = cn; nc = in_;
nc = nc; nc = cc; nc = ic;
nc = ni; nc = ci; nc = ii;

void fun(T, size_t N)(Slice!(const(T)*, N, Universal) sl)
{
    //...
}

fun(nn); fun(cn); fun(in_);
fun(nc); fun(cc); fun(ic);
fun(ni); fun(ci); fun(ii);

static assert(is(typeof(cn[]) == typeof(nc)));
static assert(is(typeof(ci[]) == typeof(ni)));
static assert(is(typeof(cc[]) == typeof(nc)));

static assert(is(typeof(in_[]) == typeof(ni)));
static assert(is(typeof(ii[]) == typeof(ni)));
static assert(is(typeof(ic[]) == typeof(ni)));

ni = ci[];
ni = in_[];
ni = ii[];
ni = ic[];

Meta