isRandomAccessRange!(const(size_t[])) is false ... ?

Jonathan M Davis jmdavisProg at gmx.com
Thu Sep 12 16:05:37 PDT 2013


On Friday, September 13, 2013 00:46:20 Joseph Rushton Wakeling wrote:
> On 13/09/13 00:15, Jonathan M Davis wrote:
> > Nope. It's correct. Because it's const, you can't call popFront on it or
> > any of the other mutating range operations. So, it's not a valid range.
> D'oh! :-P
> 
> I gave the assert as an example, but this one snuck up on me rather
> unexpectedly -- I'd got a class method that was something like this:
> 
> auto foo(in size_t n) @safe const nothrow pure
> {
> return buf[n]; // where buf is a size_t[][]
> }
> 
> ... and it turned out that foo was trying to return a const(size_t[]) rather
> than a size_t[].

At present, const and ranges basically don't mix. Slicing an array results in 
a tail-const slice, which allows you to get away with using const arrays and 
then slice them when you need to operate on a range which is const, but it's 
actually really, really hard to define opSlice in a way that that works for 
user-defined types. We really need to fix that, but for now, it generally means 
that you don't use const if you're using ranges (which of course is not what 
we want, but we're stuck for the moment).

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list