monarch_dodra wrote:
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);}
}
Why not use size_t or ulong as parameter? This way all smaller types
will be implicitly converted.
ushort opIndex(size_t n){return cast(ushort)(first + n);}