On Friday, 26 January 2018 at 14:59:09 UTC, Simen Kjærås wrote:
what is N here? You're declaring it to be an int value in the template<> definition, and then use it as a type in the function definition.

Oops again :) Should've been typename N (where N is some integral type).

Not exactly. range.front will assert after the last popFrontN (since the range is empty).

Ya, sorry, realized this a bit after I posted.

It's trying to combine 3 and 4 I think, but it fails because this is allowed in D:

int a;
a = false;

Ah true, so it's more of a is(ElementType!R : bool) check?


You'll want to pass the range as ref. hasLvalueElements checks if the elements have lvalue semantics

Doh, of course. It's in the name even!

import std.range;

struct R {
    int[3] elements;
    int index;

    ref auto front() {
        return elements[index];
    }

    void popFront() {
        ++index;
    }

    bool empty() {
        return index >= elements.length;
    }
}

unittest {
    assert(hasLvalueElements!(R));
    auto a = R([1,2,3], 0);
    auto b = a;
    b.front = 4;
    assert(a.elements == [1,2,3]);
    assert(b.elements == [4,2,3]);
}

As we can see, b is a complete copy of a, and changing b does not change a. The exact same behavior would occur if a was passed by value to a function.

--
  Simen

Thanks for the input!


Reply via email to