Need for (C++20) Contiguous Range

Petar Petar
Fri Oct 9 13:05:44 UTC 2020


On Friday, 9 October 2020 at 09:52:12 UTC, Ola Fosheim Grøstad 
wrote:
> On Thursday, 8 October 2020 at 16:42:51 UTC, Steven 
> Schveighoffer wrote:
>> It should just be:
>>
>> enum isDynamicArray(T) = is(T == U[], U);
>
> So if I create my owner container that has an opCast overload 
> to a slice of the underlying buffer, would isDynamicArray be 
> true for my container?
>
> If not you need a new test.

I think Steven's idea is not that the container itself is a 
contiguous range, but that slicing it would return a contiguous 
range, which itself is a D slice. I.e. you need to implement 
opSlice() (or opIndex()) with no parameters, which should return 
a slice of the container's contents.

For example, std.array.Appender implements it:
https://dlang.org/phobos/std_array#.Appender.opSlice

However std.container.array doesn't and instead returns a custom 
Range struct.
https://dlang.org/phobos/std_container_array#.Array.opSlice

On the other hand, std.container.array has opSliceAssign:
https://dlang.org/phobos/std_container_array#.Array.opSliceAssign

which covers part of the need for C++'s contiguous range concept.

Another interesting thing to note is that mir-algorithm (which 
implements multi-dimensional slices) has specifically the concept 
of ContiguousSlices:
http://mir-algorithm.libmir.org/mir_ndslice_slice.html#SliceKind
http://mir-algorithm.libmir.org/mir_ndslice_topology.html#.assumeContiguous
http://mir-algorithm.libmir.org/mir_ndslice_traits.html
http://mir-algorithm.libmir.org/mir_ndslice_slice.html#Slice 
(check the "Internal Binary Representation" section)


More information about the Digitalmars-d mailing list