Variadic Template: cast problem

Namespace rswhite4 at googlemail.com
Sat Aug 4 07:16:50 PDT 2012


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.


More information about the Digitalmars-d-learn mailing list