Rewriting a c++ template to D (replacing iterator with ranges "properly")

aliak something at something.com
Fri Jan 26 14:16:04 UTC 2018


It basically steps through in a stride and sets the checkpoints 
to false.

C++:
template <typename /*RandomAccessIterator*/ It, int /*Integer*/ N>
void mark(It begin, It end, N step) {
   assert(begin != end)
   *begin = false;
   while (end - begin > step) {
     begin = begin + step;
     *begin = false;
   }
}

For D this is what I figured would be the way?

void mark(R, N)(auto ref R range, N step)
if (
   /* 1 */ isIntegral!N
   /* 2 */ && isRandomAccessRange!R
   /* 3 */ && is(ElementType!R == bool)
   /* 4 */ && hasAssignableElements!R
) {
   range.front = false;
   while (!range.empty) {
     range.popFrontN(N);
     range.front = false;
   }
}

I have a more specific question too:

1) I've seen some phobos code checking for assignability like 
this:

   is(typeof(range.front = false))

... is that an advantage of that over hasAssignableElements? Or 
is that just basically combining constraints 3 and 4 which I have 
above?

2) Say I wanted to restrict to only lvalue ranges passed in as 
inputs. Does that mean I use hasLvalueElements as a constraint or 
is remove the "auto" and just have a ref parameter sufficient?

Cheers


More information about the Digitalmars-d-learn mailing list