"IndexType" for ranges
monarch_dodra
monarchdodra at gmail.com
Tue Oct 2 09:45:01 PDT 2012
On Tuesday, 2 October 2012 at 16:09:16 UTC, Peter Alexander wrote:
> On Tuesday, 2 October 2012 at 13:17:45 UTC, monarch_dodra wrote:
>> If you've ever worked on a template that needs to index a
>> range, you may have run into this problem: What is the type
>> you should use to index an RA range?
>
> Forgive my ignorance. What's wrong with size_t?
This is what happens when you use size_t:
//----
import std.range;
import std.algorithm;
struct ZeroToTen
{
ushort first = 0;
ushort last = 10;
@property bool empty(){return first == last;}
@property ushort front(){return first;}
void popFront(){++first;}
@property ushort back(){return last;}
void popBack(){--last;}
@property ZeroToTen save(){return this;}
@property ushort length(){return cast(ushort)(last - first);}
ushort opIndex(ushort n){return cast(ushort)(first + n);}
}
void main()
{
ZeroToTen ztt;
static assert(hasLength!ZeroToTen); //OK: normal
static assert(isRandomAccess!ZeroToTen); //Ok... But I don't
like where this is going...
auto r = assumeSorted(ztt); //DERP!
}
//----
\src\phobos\std\range.d(6909): Error: function
main.ZeroToTen.opIndex (ushort n) is not callable using argument
types (uint)
\src\phobos\std\range.d(6909): Error: cannot implicitly convert
expression (i) of type uint to ushort
\src\phobos\std\range.d(7346): Error: template instance
std.range.SortedRange!(ZeroToTen,"a < b") error instantiating
//----
More information about the Digitalmars-d
mailing list