#### Archived

This topic is now archived and is closed to further replies.

# Templates variables

This topic is 5532 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

hi.. Just a quick stupid question. I''ve been thinking of changing my 3D vector class to templated one so I don''t have to write a 2D and 4D one. But I have one problem with it. For verctor values I use union like : union { struct { float x,y,z; } float v[3]; } Now the trouble is if I change this to templated verison I can''t use slomething like myVector.y = 5; but I have to write myVector.v[1] = 5; Is there any way I could use templates and still use .x notation? You should never let your fears become the boundaries of your dreams.

##### Share on other sites
quote:
Original post by _DarkWIng_
hi..

Just a quick stupid question. I''ve been thinking of changing my 3D vector class to templated one so I don''t have to write a 2D and 4D one. But I have one problem with it. For verctor values I use union like :

union {
struct {
float x,y,z;
}
float v[3];
}

Now the trouble is if I change this to templated verison I can''t use slomething like myVector.y = 5; but I have to write myVector.v[1] = 5;

Is there any way I could use templates and still use .x notation?

You should never let your fears become the boundaries of your dreams.

damn right!

well, u are right, this should be possible, but c++ makes it rather hard.

here is a 2 ideas:
1)
you could cast you float[1/2/3/4] into a struct vector{float x, y, z, w;}; and just be careful u dont dereference the w of a float[2] (->SIGSEV).
2)
subclass, use an abstract class to combine all that the vectors have in common (also operations like dot product, crossproduct, norm etc.) an then u soubclass with classes that explicitely store the values in named variables

in both approaches you get a problem when you have arrays, coz the compiler might assume the wrong size of the struct (or just refuse to compile), with the abstract class approach this is more subtle though and you might find a way around.

good luck

daniel

##### Share on other sites
You could have functions X() Y() etc that return a reference to the appropriate entry in the array.

float& Y() { return v[1]; }

##### Share on other sites
You could partially specialize (i.e. doesn''t work on MSVC6):

  template<typename T, int N>struct vector   {   Float v[N];      Float& operator[](size_t i)      {return this->v[i];}   const Float& operator[](size_t i) const      {return this->v[i];}   };template<typename T>struct vector<T, 2>   {   union      {      struct         {         Float x,y;         };      Float v[2];      };   Float& operator[](size_t i)      {return this->v[i];}   const Float& operator[](size_t i) const      {return this->v[i];}   };template<typename T>struct vector<T, 3>   {   union      {      struct         {         Float x,y,z;         };      Float v[3];      };   Float& operator[](size_t i)      {return this->v[i];}   const Float& operator[](size_t i) const      {return this->v[i];}   };template<typename T>struct vector<T, 4>   {   union      {      struct         {         Float w,x,y,z;         };      Float v[4];      };   Float& operator[](size_t i)      {return this->v[i];}   const Float& operator[](size_t i) const      {return this->v[i];}   };

##### Share on other sites
Magmai Kai Holmlor : hmmm.. I''ll try the template specialization. I did''t know you could "override" whole class definition, I tought you could just change function definitions. I just hope it will work.

AP : I dont'' realy understand what do you mean by #2. I should cast where?

One other question (a bit unrelated): Is there a speed difference if I use this->x inside member functions instead of this->v[0]. Let''s say in dot product I have

return x*x+y*y+z*z;

will this be compiled different than

return v[0]*v[0]+v[1]*v[1]+v[2]*v[2];

or will compiler optimize it to same thing?

You should never let your fears become the boundaries of your dreams.

1. 1
Rutin
24
2. 2
3. 3
4. 4
JoeJ
18
5. 5

• 14
• 14
• 11
• 11
• 9
• ### Forum Statistics

• Total Topics
631757
• Total Posts
3002142
×