Enum member hasSlicing
Determines whether R is a forward range with slicing support
(R[i .. j]
).
enum hasSlicing(R)
= (is(T == R) || isInfinite!R) && hasLength!T;
For finite ranges, the result of opSlice()
must be of the same type as the
original range. If the range defines opDollar, it must support subtraction.
For infinite ranges, the result of opSlice()
must be of the same type as
the original range only if it defines opDollar()
. Otherwise it can be any
forward range.
For both finite and infinite ranges, the result of opSlice()
must have
length.
Parameters
Name | Description |
---|---|
R | The type to be tested. |
Returns
true if R supports slicing, false otherwise.
Example
static assert(hasSlicing!(int[]));
static assert(hasSlicing!(const(int)[]));
static assert(hasSlicing!(dstring));
static assert(hasSlicing!(string));
static assert(!hasSlicing!(const int[]));
static assert(!hasSlicing!(void[]));
struct A
{
int front() @nogc nothrow pure @safe
{
return 0;
}
void popFront() @nogc nothrow pure @safe
{
}
bool empty() const @nogc nothrow pure @safe
{
return false;
}
typeof(this) save() @nogc nothrow pure @safe
{
return this;
}
@property size_t length() const @nogc nothrow pure @safe
{
return 0;
}
typeof(this) opSlice(const size_t i, const size_t j)
pure nothrow @safe @nogc
{
return this;
}
}
static assert(hasSlicing!A);
struct B
{
struct Dollar
{
}
int front() @nogc nothrow pure @safe
{
return 0;
}
void popFront() @nogc nothrow pure @safe
{
}
bool empty() const @nogc nothrow pure @safe
{
return false;
}
typeof(this) save() @nogc nothrow pure @safe
{
return this;
}
@property size_t length() const @nogc nothrow pure @safe
{
return 0;
}
@property Dollar opDollar() const @nogc nothrow pure @safe
{
return Dollar();
}
typeof(this) opSlice(const size_t i, const Dollar j)
pure nothrow @safe @nogc
{
return this;
}
}
static assert(!hasSlicing!B);
struct C
{
int front() @nogc nothrow pure @safe
{
return 0;
}
void popFront() @nogc nothrow pure @safe
{
}
enum bool empty = false;
typeof(this) save() @nogc nothrow pure @safe
{
return this;
}
typeof(this) opSlice(const size_t i, const size_t j)
pure nothrow @safe @nogc
{
return this;
}
}
static assert(!hasSlicing!C);
struct D
{
struct Range
{
int front() @nogc nothrow pure @safe
{
return 0;
}
void popFront() @nogc nothrow pure @safe
{
}
bool empty() const @nogc nothrow pure @safe
{
return true;
}
typeof(this) save() @nogc nothrow pure @safe
{
return this;
}
@property size_t length() const @nogc nothrow pure @safe
{
return 0;
}
}
int front() @nogc nothrow pure @safe
{
return 0;
}
void popFront() @nogc nothrow pure @safe
{
}
enum bool empty = false;
typeof(this) save() @nogc nothrow pure @safe
{
return this;
}
Range opSlice(const size_t i, const size_t j)
pure nothrow @safe @nogc
{
return Range();
}
}
static assert(hasSlicing!D);