Requirements for Slicing Ranges

Paul Backus snarwin at gmail.com
Mon Sep 2 11:53:55 UTC 2024


On Monday, 2 September 2024 at 09:59:04 UTC, Sebastiaan Koppe 
wrote:
>
> I briefly wondered how it would work if the range is 
> non-copyable, since taking a slice is effectively taking a copy 
> of the underlying data the range captures (and its lifetime).

Taking a slice does not necessarily copy the range, nor does it 
necessarily copy the range's elements.

For example:

     struct DontCopyMe
     {
         this(this) { assert(0); }
     }

     void main()
     {
         auto r1 = [DontCopyMe(), DontCopyMe(), DontCopyMe()];
         auto r2 = r1[1 .. $];
         // No AssertError => elements not copied
         assert(r1 != r2); // Not equal => r2 is not a copy of r1
     }


More information about the Digitalmars-d mailing list