[Issue 5055] New: hasAssignableElements etc only check forward range primitives

d-bugmail at puremagic.com d-bugmail at puremagic.com
Fri Oct 15 00:37:33 PDT 2010


http://d.puremagic.com/issues/show_bug.cgi?id=5055

           Summary: hasAssignableElements etc only check forward range
                    primitives
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Keywords: accepts-invalid
          Severity: normal
          Priority: P2
         Component: Phobos
        AssignedTo: nobody at puremagic.com
        ReportedBy: yebblies at gmail.com


--- Comment #0 from yebblies <yebblies at gmail.com> 2010-10-15 00:36:58 PDT ---
Most of the hasSomething templates in std.range should check that range.back
and/or range[i] fill the requirements. hasMobileElements already does this.

Also, should back/opIndex always return the same type as front?
If so, this should be added to the definitions of isBidirectionalRange and
isRandomAccesRange.

List:
 hasSwappableElements
 hasAssignableElements
 hasLvalueElements


Possible Implementations:


template hasSwappableElements(R)
{
    enum bool hasSwappableElements = isForwardRange!(R) && is(typeof(
    {
        R r;
        swap(r.front, r.front);             // can swap elements of the range
    })) && (!isBidirectionalRange!R || is(typeof(
    {
        R r;
        swap(r.back, r.back);
    }))) && (!isRandomAccessRange!R || is(typeof(
    {
        R r;
        swap(r[0], r[0]);
    })));
}


template hasAssignableElements(R)
{
    enum bool hasAssignableElements = isInputRange!(R) && is(typeof(
    {
        R r;
        auto e = r.front;
        r.front = e;                       // can assign elements of the range
    })) && (!isBidirectionalRange!R || is(typeof(
    {
        R r;
        auto e = r.back;
        r.back = e;
    }))) && (!isRandomAccessRange!R || is(typeof(
    {
        R r;
        auto e = r[0];
        r[0] = e;
    })));
}



template hasLvalueElements(R)
{
    enum bool hasLvalueElements =
        isInputRange && is(typeof(&R.init.front()) == ElementType!(R)*) &&
        (!isBidirectionalRange!R || is(typeof(&R.init.back()) ==
ElementType!(R)*)) &&
        (!isRandomAccessRange!R || is(typeof(&R.init[0]) == ElementType!(R)*));
}



Adding the appropriate asserts to ElementType, isBidirectionalRange and
isRandomAccessRange should prevent ranges with mismatched element types from
being used.

static assert(!isBidirectionalRange!R || is(typeof({return R.init.front();}())
== typeof({return R.init.back();}()), "Members front and back must have the
same type");
static assert(!isRandomAccessRange!R || is(typeof({return R.init.front();}())
== typeof({return R.init[0];}()), "Members front and opIndex must have the same
type");

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list