Implement an N deminsional mathematical vector.

• 05-15-2011
User Name:
Implement an N deminsional mathematical vector.
How could I make a constructor that accepts a templated number of floats?

Code:

```template<int N> class vector {     vector(float _1, ..., float _N); // ??? };```
• 05-15-2011
User Name:
I'm using C++0x, btw,
• 05-15-2011
tabstop
To be honest, this sounds more like a job for ... then for templates.
• 05-15-2011
User Name:
Is it possible to limit the number of arguments with ...?
• 05-15-2011
tabstop
Quote:

Originally Posted by User Name:
Is it possible to limit the number of arguments with ...?

There's probably an internal limit of something outrageous like 16384, but I don't know of a way to limit it otherwise. (Flip side: same would be true of your template solution if it existed.)
• 05-15-2011
User Name:
I realize there is a limit to the size of the stack. :)

How would you define an N dimensional vector?
• 05-15-2011
tabstop
Really depends on the context of what I'm using these for. Since you wrote "mathematical vector" I'm assuming you're looking for something you want to be able to do operations like + and * on. Since you can't ever really operate with two different sizes at the same time, I don't know that I'd worry about making them compatible unless you have to make a bunch of different sizes.
• 05-15-2011
User Name:
That's a good idea. I was planning on only allowing N vectors to be used with N vectors, it was just the generalization of the math that I was after. I'll still use the template, but leave it absract and specialize the ones I actually plan to use.

Like so:
Code:

```template<int N> class vector {         public:         vector(...) = 0; }; template<> class vector<3> {         public:         vector(float, float, float)         {         }                 vector<3> operator+(const vector<3>&){} };```
Thanks for your time and help.
• 05-16-2011
Elysia
For taking a variable argument list, you might use std::initializer_list if it's supported.