ndslice of an array structure member?

Jack Stouffer via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Dec 21 17:13:54 PST 2015


On Monday, 21 December 2015 at 23:59:07 UTC, Jay Norwood wrote:
> I'm trying to learn ndslice.  It puzzles me why t3 compiles ok, 
> but t4 causes a compiler error in the example below.  Should I 
> be able to slice a struct member that is an array?
>
> import std.stdio;
> import std.experimental.ndslice;
> import std.experimental.ndslice.iteration: transposed;
> struct sample{
> 	ulong [10] core_ctr;
> }
>
> struct block{
> 	ulong[60] samples;
> }
>
> void main() {
> 	auto a1 = new sample[60];
> 	auto t3 = a1.sliced!(ReplaceArrayWithPointer.no)(3,4,5);
> 	auto b1 = new block;
> 	auto t4 = 
> b1.samples.sliced!(ReplaceArrayWithPointer.no)(3,4,5);
> }

The problem is that t3 is slicing a1 which is a dynamic array, 
which is a range, while t4 is trying to slice a static array, 
which is not a range.

The ranges primitive popFront mutates the length of the range, so 
static arrays cannot be used as ranges. But, if you take a slice 
of b1.samples, you can use that as a range.

auto t4 = b1.samples[].sliced!(ReplaceArrayWithPointer.no)(3,4,5);

See the section on ranges on this page for more general info on 
ranges: http://dlang.org/overview.html


More information about the Digitalmars-d-learn mailing list