www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Synax for variadic template

reply Alex <sascha.orlov gmail.com> writes:
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 :)
Sep 01 2017
parent reply Nicholas Wilson <iamthewilsonator hotmail.com> writes:
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/
Sep 01 2017
parent Alex <sascha.orlov gmail.com> writes:
On Friday, 1 September 2017 at 10:01:16 UTC, Nicholas Wilson 
wrote:
 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/
Ok... I think I need some type of soa, yes. But in the article, there is too much of effort done, to convert types to arrays, at least at the stage of my code... At the end, if I compare the amount of code in the article and just "[]" more, as for the part of my example that works... Thanks for the cool link, nevertheless... Cool to see, how it should be done :)
Sep 01 2017