Synax for variadic template
Nicholas Wilson via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Fri Sep 1 03:01:16 PDT 2017
On Friday, 1 September 2017 at 09:38:59 UTC, Alex wrote:
> Hi all!
> Say, I have
>
> struct A(T...)
> {
> T arr;
> }
>
> struct B(T...)
> {
> T[] arr;
> }
>
> void main()
> {
> A!(int[], double[]) a;
> a.arr[0] ~= 5;
> a.arr[0] ~= 6;
> static assert(!__traits(compiles, a.arr[0] ~= 3.5));
> a.arr[1] ~= 19.8;
>
> assert(a.arr[0] == [5,6]);
> assert(a.arr[1] == [19.8]);
> assert(a.arr[1].length == 1);
> assert(a.arr[0].length == 2);
>
> B!(int, double) b;
> //b.arr[0] ~= 5;
> }
>
> While struct A behaves like expected, how to get by with B?
> What I want is just to abstract the array property out of
> template arguments...
>
> Thanks in advance :)
b.arr refers to an `(AliasSeq!(int, double))[]`, so with
`b.arr[0] ~= 5;` you are trying to append a integer to an array
of pairs of ints and doubles, which you can't do.
b.arr[0] ~= ElementType!(typeof(b.arr))(5,42.0);
should work (I hope, not tested) but you cannot append only the
int.
If you are trying to abstract the array for struct of array vs.
array of struct the take a look at
https://maikklein.github.io/post/soa-d/
More information about the Digitalmars-d-learn
mailing list