www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Variadic Template: cast problem

reply "Namespace" <rswhite4 googlemail.com> writes:
This code http://dpaste.dzfl.pl/6caed813 does only compile if i 
comment out the "Clone" method. Why? o.O
Aug 04 2012
parent reply Dmitry Olshansky <dmitry.olsh gmail.com> writes:
On 04-Aug-12 17:57, Namespace wrote:
 This code http://dpaste.dzfl.pl/6caed813 does only compile if i comment
 out the "Clone" method. Why? o.O
Because it calls constructor and fails? I've done substitution for you: this(const vec!(float,2) values) { foreach (index, val; values) { this.values[index] = cast(T) val; } } No big wonder. I'd try to fix Clone to: { vec!(T, dim) tmp = void; tmp.values = this.values; return tmp; } -- Dmitry Olshansky
Aug 04 2012
parent reply "Namespace" <rswhite4 googlemail.com> writes:
On Saturday, 4 August 2012 at 14:05:32 UTC, Dmitry Olshansky 
wrote:
 On 04-Aug-12 17:57, Namespace wrote:
 This code http://dpaste.dzfl.pl/6caed813 does only compile if 
 i comment
 out the "Clone" method. Why? o.O
Because it calls constructor and fails? I've done substitution for you: this(const vec!(float,2) values) { foreach (index, val; values) { this.values[index] = cast(T) val; } } No big wonder. I'd try to fix Clone to: { vec!(T, dim) tmp = void; tmp.values = this.values; return tmp; }
Then temp is empty/null and you cannot assign anything to it. Furthermore temp would get a reference to the original array. And why it fails? As i see, Clone calls this opCall method: [code] static vec!(T, dim) opCall(U, ubyte dim)(const vec!(U, dim) v) { return vec!(T, dim)(v.values); } [/code] A simple workaround is to write [code] this(U...)(U values) { T[] temp; foreach (val; values) { temp ~= val; } foreach (index, val; temp) { this.values[index] = cast(T) val; } } [/code] instead of [code] this() { foreach (index, val; values) { this.values[index] = cast(T) val; } } [/code] But that seems weird to me.
Aug 04 2012
parent reply Dmitry Olshansky <dmitry.olsh gmail.com> writes:
n 04-Aug-12 18:16, Namespace wrote:
 On Saturday, 4 August 2012 at 14:05:32 UTC, Dmitry Olshansky wrote:
 On 04-Aug-12 17:57, Namespace wrote:
 This code http://dpaste.dzfl.pl/6caed813 does only compile if i comment
 out the "Clone" method. Why? o.O
Because it calls constructor and fails? I've done substitution for you: this(const vec!(float,2) values) { foreach (index, val; values) { this.values[index] = cast(T) val; } } No big wonder. I'd try to fix Clone to: { vec!(T, dim) tmp = void; tmp.values = this.values; return tmp; }
Then temp is empty/null and you cannot assign anything to it. Furthermore temp would get a reference to the original array.
Doh. I somehow totally expected fixed size vector to be value type (struct).
 And why it fails?
 As i see, Clone calls this opCall method:
 [code]
 static vec!(T, dim) opCall(U, ubyte dim)(const vec!(U, dim) v) {
      return vec!(T, dim)(v.values);
 }
Then more simple thing is: return vec!(T, dim)(v.values[]); Note the []. Static arrays are not convertible to dynamic implicitly. Your use of ~= probably does append all of one static array elements in one operation. To check it insert writeln in each iteration of your foreach loops, you might be surprised. -- Dmitry Olshansky
Aug 04 2012
parent "Namespace" <rswhite4 googlemail.com> writes:
http://dpaste.dzfl.pl/ff58ffc5

I'm even more confused. o.O
Aug 04 2012