Swap front for char[] input ranges

Basile B. via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Dec 19 04:17:54 PST 2016


On Monday, 19 December 2016 at 10:41:46 UTC, RazvanN wrote:
> Hi,
>
> I have a function which accepts 2 input Ranges and swaps the 
> first element in Range1 with the first element in Range2. The 
> swapping code looks something like this :
>
>      static if (is(typeof(swap(r1.front, r2.front))))
>      {
>          swap(r1.front, r2.front);
>      }
>      else
>      {
>          auto t1 = moveFront(r1), t2 = moveFront(r2);
>          auto tmp1 = r1.front;
>          auto tmp2 = r2.front;
>          r1.front = move(t2);
>          r2.front = move(t1);
>      }
>
> This code works for most cases, except when 2 char[] are passed.
> In that case, the compilation fails with error messages which 
> state
> that r1.front and r2.front are not Lvalues. So, my question is:
> how can I swap the 2 elements since in the case of char[] input 
> ranges
> the front property does not return a reference?

does it work if you cast the 2 char[] as 2 ubyte[] ?
If so, it's a problem with narrow strings and utf8 decoding...i.e 
there's no guarantee that each code unit in r1 matches to a code 
unit in r2.


More information about the Digitalmars-d-learn mailing list