Iterators in structs
Paul Backus
snarwin at gmail.com
Thu Jun 25 18:55:07 UTC 2020
On Thursday, 25 June 2020 at 18:47:42 UTC, repr-man wrote:
> struct ChunksOf(R)
> {
> Chunks!R iter;
>
> this(R r, size_t width)
> {
> this.iter = r.chunks(width);
> assert(is(typeof(iter) == Chunks!R));
> }
> }
>
> This works, only if I change the declaration of x in main() to:
>
> auto x = ChunksOf!(chain(a[], b[]))(chain(a[], b[]), 2);
>
> This requires me to pass the iterator as a template parameter
> and a regular parameter. Since this is a bit redundant, and I
> wanted to know if there was a better way to do it.
>
> Thanks for the help!
The usual solution is to create a helper function:
ChunksOf!R chunksOf(R)(R r, size_t width)
{
return ChunksOf!R(r, width);
}
The compiler can infer template arguments for function calls, so
you can call it like this:
auto x = chunksOf(chain(a[], b[]), 2);
More information about the Digitalmars-d-learn
mailing list