View source code
Display the source code in std/range/primitives.d from which this page was generated on github.
Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using local clone.

Enum member std.range.primitives.isInputRange

Returns true if R is an input range. An input range must define the primitives empty, popFront, and front. The following code should compile for any input range.

enum isInputRange(R) = is(typeof(R.init) == R) && is(typeof((R r) { return r.empty; } (R.init)) == bool) && (is(typeof((return ref R r) => r.front)) || is(typeof((return ref R r) ref => r.front))) && !is(typeof((R r) { return r.front; } (R.init)) == void) && is(typeof((R r) => r.popFront));
enum isInputRange(R, E) = .isInputRange!R && isQualifierConvertible!(ElementType!R, E);
R r;              // can define a range object
if (r.empty) {}   // can test for empty
r.popFront();     // can invoke popFront()
auto h = r.front; // can get the front of the range of non-void type

The following are rules of input ranges are assumed to hold true in all Phobos code. These rules are not checkable at compile-time, so not conforming to these rules when writing ranges or range based code will result in undefined behavior.

  • r.empty returns false if and only if there is more data available in the range.
  • r.empty evaluated multiple times, without calling r.popFront, or otherwise mutating the range object or the underlying data, yields the same result for every evaluation.
  • r.front returns the current element in the range. It may return by value or by reference.
  • r.front can be legally evaluated if and only if evaluating r.empty has, or would have, equaled false.
  • r.front evaluated multiple times, without calling r.popFront, or otherwise mutating the range object or the underlying data, yields the same result for every evaluation.
  • r.popFront advances to the next element in the range.
  • r.popFront can be called if and only if evaluating r.empty has, or would have, equaled false.

Also, note that Phobos code assumes that the primitives r.front and r.empty are Ο(1) time complexity wise or "cheap" in terms of running time. Ο() statements in the documentation of range functions are made with this assumption.

See Also

The header of std.range for tutorials on ranges.

Parameters

NameDescription
R type to be tested
E if present, the elements of the range must be qualifier-convertible to this type

Returns

true if R is an input range (possibly with element type E), false if not

Example

struct A {}
struct B
{
    void popFront();
    @property bool empty();
    @property int front();
}
static assert(!isInputRange!A);
static assert( isInputRange!B);
static assert( isInputRange!(int[]));
static assert( isInputRange!(char[]));
static assert(!isInputRange!(char[4]));
static assert( isInputRange!(inout(int)[]));
static assert(!isInputRange!(int[], string));
static assert( isInputRange!(int[], int));
static assert( isInputRange!(int[], const int));
static assert(!isInputRange!(int[], immutable int));

static assert(!isInputRange!(const(int)[], int));
static assert( isInputRange!(const(int)[], const int));
static assert(!isInputRange!(const(int)[], immutable int));

static assert(!isInputRange!(immutable(int)[], int));
static assert( isInputRange!(immutable(int)[], const int));
static assert( isInputRange!(immutable(int)[], immutable int));

static struct NotDefaultConstructible
{
    @disable this();
    void popFront();
    @property bool empty();
    @property int front();
}
static assert( isInputRange!NotDefaultConstructible);

static struct NotDefaultConstructibleOrCopyable
{
    @disable this();
    @disable this(this);
    void popFront();
    @property bool empty();
    @property int front();
}
static assert(isInputRange!NotDefaultConstructibleOrCopyable);

static struct Frontless
{
    void popFront();
    @property bool empty();
}
static assert(!isInputRange!Frontless);

static struct VoidFront
{
    void popFront();
    @property bool empty();
    void front();
}
static assert(!isInputRange!VoidFront);

Authors

Andrei Alexandrescu, David Simcha, and Jonathan M Davis. Credit for some of the ideas in building this module goes to Leonardo Maffi.

License

Boost License 1.0.