How made my to!MyStruct
Zardoz
luis.panadero at gmail.com
Sat Jun 25 11:09:38 PDT 2011
Hi. I made a parametrized struct, a Vector, and I'm trying to make to! work to make conversion between Vectors with
different parametrized type.
Code :
public struct Vector(T, size_t dim_)
if (__traits(isFloating, T) ) {
static enum size_t dim = dim_; /// Vector Dimension
static assert (dim >= 2 && dim <= 4, "Not valid dimension size.");
static assert (__traits(isFloating, T), "Type not is a Float Point type."); // Ok, redundant
static assert (is(T : real), "Type not is like a Float Point type.");
union {
private T[dim] coor; /// Vector coords like Array
struct {
static if( dim >= 1) T x;
static if( dim >= 2) T y;
static if( dim >= 3) T z;
static if( dim >= 4) T w;
}
}
// Constructors and other methods/stuff....
}
Actually I saw std.conv code and I see that to! call to a parametrized funtion called toImpl(T,S) (S s).
I made my own toImpl for convert a Vector to another Vector and I checked that it works calling directly :
/**
* to converto a vector to other vector
*/
T toImpl(T, S)(S s)
if (!implicitlyConverts!(S, T) && isVector!T && isVector!S )
{
static assert (T.dim >= S.dim, "Original Vector bigger that destiny Vector");
T newVector; auto i = 0;
static if (is (typeof(newVector.x) == typeof(s.x))) {
for (; i < S.dim; i++)
newVector.coor[i] = s.coor[i];
} else {
for (; i < S.dim; i++)
newVector.coor[i] = to!(typeof(newVector.x))(s.coor[i]);
}
for (; i< T.dim; i++) // Expands a small vector to a bigger dimension with 0 value in extra dimension
newVector.coor[i] = 0;
return newVector;
}
But when I try to use to! (for example to!Vector(double, 4) (other_Vector)), I get a error from srd.conv :
/usr/include/d/dmd/phobos/std/conv.d(99): Error: template std.conv.toImpl(T,S) if (!implicitlyConverts!(S,T) &&
isSomeString!(T) && isInputRange!(Unqual!(S)) && isSomeChar!(ElementType!(S))) does not match any function template
declaration
/usr/include/d/dmd/phobos/std/conv.d(99): Error: template std.conv.toImpl(T,S) if (!implicitlyConverts!(S,T) &&
isSomeString!(T) && isInputRange!(Unqual!(S)) && isSomeChar!(ElementType!(S))) cannot deduce template function from
argument types !(Vector!(double,4))(Vector!(float,2))
/usr/include/d/dmd/phobos/std/conv.d(99): Error: template instance errors instantiating template
src/vector.d(139): Error: template instance std.conv.to!(Vector!(double,4)).to!(Vector!(float,2)) error instantiating
What I'm doing wrong ???
More information about the Digitalmars-d-learn
mailing list