std.range.interfaces : InputRange moveFront

Ali Çehreli acehreli at yahoo.com
Thu Nov 30 06:36:12 UTC 2017


On 11/29/2017 08:31 PM, Tony wrote:
> What does the moveFront() method do in the InputRange interface?
> 
> std.range.interfaces : InputRange.moveFront()

move is an operation that transfers the state of the source to the 
destination. The front element becomes its .init value and its previous 
values is returned by moveFront().

The important bit is that, the element is *not* copied:

import std.range;

struct S {
     int i;
     bool is_a_copy = false;
     this(this) {
         is_a_copy = true;
     }
}

void main() {
     auto r =  [S(1)];

     auto a = r.front;
     assert(a.is_a_copy);       // yes, a is a copy
     assert(a.i == 1);          // as expected, 1
     assert(r.front.i == 1);    // front is still 1

     auto b = r.moveFront();
     assert(!b.is_a_copy);      // no, b is not a copy
     assert(b.i == 1);          // state is transferred
     assert(r.front.i == 0);    // front is int.init
}

Ali


More information about the Digitalmars-d-learn mailing list