Swap front for char[] input ranges
Ali Çehreli via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Mon Dec 19 04:25:02 PST 2016
On 12/19/2016 02:41 AM, RazvanN wrote:
> Hi,
>
> I have a function which accepts 2 input Ranges
As your comments make it clear below, they cannot be InputRanges.
> 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;
Obivously, tmp1 and tmp2 are unusued there. :)
> 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?
Not possible... It's ok to use something similar to the following
template constraint:
void foo(R1, R2)(R1 r1, R2 r2)
if ((hasSwappableElements!R1 && hasSwappableElements!R2) ||
(hasLvalueElements!R1 && hasLvalueElements!R2)) {
// ...
}
Ali
More information about the Digitalmars-d-learn
mailing list