Variadic Template: cast problem
Dmitry Olshansky
dmitry.olsh at gmail.com
Sat Aug 4 07:49:13 PDT 2012
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
More information about the Digitalmars-d-learn
mailing list