Getting a range over a const Container

Jonathan M Davis jmdavisProg at gmx.com
Fri Jul 20 18:02:59 PDT 2012


On Friday, July 20, 2012 17:15:53 Ellery Newcomer wrote:
> On 07/19/2012 06:09 PM, Ellery Newcomer wrote:
> > On 07/19/2012 02:51 AM, Artur Skawina wrote:
> >> Range!Node opSlice() { return Range!Node(first); }
> >> Range!(const Node) opSlice() const { return Range!(const
> >> 
> >> Node)(first); }
> 
> it looks like you could almost merge these two into one using inout, but
> I'm not sure what you do about selecting the return type.

If everywhere that you use const, you can use inout, then it may work. But the 
return type is not only a difference in constness. It's a completely different 
type.

The other problem is that the const opSlice is actually _very_ broken as far 
as range-based functions typically go. Whil opSlice doesn't technically 
require it (though it probably should), it's _very_ common for function using 
opSlice to assign the result to the original range, and that's not possible 
with this code.

I keep meaning to bring it up for discussion in the newsgroup, but it's a big 
problem. You basically need tail-const, and I don't know of any way to 
implement it, because the only way to get implicit conversions is to use alias 
this, and that results in a recursive template instantiation, which kills the 
compiler. I don't think that it's actually possible to properly implement a 
const opSlice for many ranges right now.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list