www.digitalmars.com         C & C++   DMDScript  

D - BUG: templates and structs

reply elud verat.netstrip writes:
I get error when using structs (instancied from templates) as function 
return values.

template geometry(T)
{

struct vec3
{
	T x, y, z;
	static vec3 make(T x, T y, T z) {vec3 r; r.x=x; r.y=y; r.z=z; return r;} 
// error is on this line: "identifier vec3 is not defined"
	static vec3 make(T x, T y) {return make(x,y,0);}
	T *ptr() {return &x;}

	vec3 add(T a) {return make(x+a, y+a, z+a);}
	vec3 sub(T a) {return make(x-a, y-a, z-a);}
	vec3 mul(T a) {return make(x*a, y*a, z*a);}
	vec3 div(T a) {return make(x/a, y/a, z/a);}

	vec3 neg() {return make(-x,-y,-z);}
	vec3 add(vec3 a) {return make(x+a.x, y+a.y, z+a.z);}
	vec3 sub(vec3 a) {return make(x-a.x, y-a.y, z-a.z);}
	T mul(vec3 a) {return x*a.x + y*a.y + z*a.z;}

	void addass(vec3 a) {x+=a.x; y+=a.y; z+=a.z;}
	void subass(vec3 a) {x-=a.x; y-=a.y; z-=a.z;}
}

}
Oct 26 2003
next sibling parent Patrick Down <pat codemoon.com> writes:
elud verat.netstrip wrote in news:oprxnnvbw97mnt56 news.digitalmars.com:

 I get error when using structs (instancied from templates) as function
 return values.
 
I think this bug make be a little bit more subtle. I have a similar library that compiles just fine for me when I use it as a library. E:\Programming\D\utilities>dmd -unittest main.d mathlib.d mathlib.d(16): identifier 'Vector3D' is not defined E:\Programming\D\utilities>dmd -c mathlib.d Notice there is no error when compiling just to an obj. module utilities.mathlib; import math; template vectorlib(CTYPE) { struct Vector3D { CTYPE x = 0; CTYPE y = 0; CTYPE z = 0; static Vector3D make(CTYPE c1,CTYPE c2,CTYPE c3) { Vector3D rtn; rtn.x = c1; rtn.y = c2; rtn.z = c3; return rtn; } Vector3D neg() { return Vector3D.make(-x,-y,-z); } Vector3D add(Vector3D rhs) { return Vector3D.make(x + rhs.x, y + rhs.y, z + rhs.z); } Vector3D sub(Vector3D rhs) { return Vector3D.make(x - rhs.x, y - rhs.y, z - rhs.z); } Vector3D sub_r(Vector3D lhs) { return Vector3D.make(lhs.x - x, lhs.y - y, lhs.z - z); } Vector3D mul(CTYPE rhs) { return Vector3D.make(x*rhs,y*rhs,z*rhs); } Vector3D div(CTYPE rhs) { return Vector3D.make(x/rhs,y/rhs,z/rhs); } Vector3D div_r(CTYPE rhs) { assert(false); return Vector3D.make(0,0,0); } Vector3D addass(Vector3D rhs) { x += rhs.x; y += rhs.y; z += rhs.z; return *this; } Vector3D subass(Vector3D rhs) { x -= rhs.x; y -= rhs.y; z -= rhs.z; return *this; } Vector3D mulass(CTYPE rhs) { x *= rhs; y *= rhs; z *= rhs; return *this; } Vector3D divass(CTYPE rhs) { x /= rhs; y /= rhs; z /= rhs; return *this; } CTYPE length2() { return x*x + y*y + z*z; } CTYPE length() { return sqrt(length2()); } } Vector3D normalize(Vector3D vec) { CTYPE len = vec.length(); return Vector3D.make(vec.x/len,vec.y/len,vec.z/len); } Vector3D cross(Vector3D vec1,Vector3D vec2) { return Vector3D.make( vec1.y*vec2.z - vec1.z*vec2.y, vec1.z*vec2.x - vec1.x*vec2.z, vec1.x*vec2.y - vec1.y*vec2.x); } CTYPE dot(Vector3D vec1,Vector3D vec2) { return vec1.x*vec2.x + vec1.y*vec2.y + vec1.z*vec2.z ; } CTYPE distance(Vector3D vec1,Vector3D vec2) { return (vec1 - vec2).length(); } } unittest { alias instance vectorlib(double).Vector3D vector; alias instance vectorlib(double).cross cross; alias instance vectorlib(double).dot dot; alias instance vectorlib(double).distance distance; vector xAxis = vector.make(1,0,0); vector yAxis = vector.make(0,1,0); vector zAxis = vector.make(0,0,1); vector comp = xAxis + yAxis + zAxis; assert(comp.x == 1 && comp.y == 1 && comp.z == 1); vector scaled = comp*5; assert(scaled.x == 5 && scaled.y == 5 && scaled.z == 5); vector leng = xAxis*2 + yAxis*2 + zAxis; assert(leng.length() == 3.0); vector xyCross = cross(xAxis,yAxis); assert(xyCross.z > 0 && xyCross.x == 0 && xyCross.y == 0); vector yzCross = cross(yAxis,zAxis); assert(yzCross.x > 0 && yzCross.z == 0 && yzCross.y == 0); vector xzCross = cross(zAxis,xAxis); assert(xzCross.y > 0 && xzCross.x == 0 && xzCross.z == 0); assert(dot(xAxis,yAxis) == 0); assert(distance(xAxis,xAxis+yAxis) == 1.0); }
Oct 26 2003
prev sibling parent reply "Julio César Carrascal Urquijo" <adnoctum phreaker.net> writes:
I think you can't use a struct as a return value. Since they are
instantiated in the stack, when the function returns, the memory they use
will be reclaimed by the OS.

I'm not sure, though. You should ask someone more experienced in D.
Oct 26 2003
parent "Sean L. Palmer" <palmer.sean verizon.net> writes:
This is not true.  In D (as in C) you can return structs from functions.
There may be a bug somewhere, but it's not because of this.

Sean

"Julio César Carrascal Urquijo" <adnoctum phreaker.net> wrote in message
news:bnhhdp$2ag9$1 digitaldaemon.com...
 I think you can't use a struct as a return value. Since they are
 instantiated in the stack, when the function returns, the memory they use
 will be reclaimed by the OS.

 I'm not sure, though. You should ask someone more experienced in D.
Oct 26 2003