Constraining the length of std::vector<double>

R

Rune Allnor

Hi all.

I have an application where I need to do some computations on
vectors of numerical data. Specifically, the routine requires an
N-dimensional point and an N-dimensional vector as input.

The naive approach is to implement this as two instances
of std::vector<double>. However, I would like to include some
error-checking on the lengths of the vectors. I would at least
detect if the vectors are of different length (which would be
an error) and preferably specify by means of template
parameters exactly what the correct length should be.

It is no problem to do this at run-time and control parameters,
but this would imply a run-time overhead I would prefer
to avoid, if possible, and do all this book-keeping at
compile time instead.

Now, I would be surprised if there is a way to constrain the
sizes of the vectors at compile time, but I have to ask.

Assuming the answer is 'no', are there alternative approches
that achieve the goal?

Rune
 
S

sean_in_raleigh

I have an application where I need to do some computations on
vectors of numerical data. Specifically, the routine requires an
N-dimensional point and an N-dimensional vector as input.
[...]
It is no problem to do this at run-time and control parameters,
but this would imply a run-time overhead I would prefer
to avoid, if possible, and do all this book-keeping at
compile time instead.

How about something like this?

#include <vector>

using namespace std;

template<unsigned VectorSize>
class SizedDoubleVector : public vector<double>
{
public:
SizedDoubleVector() : vector<double>(VectorSize) {}
enum { size = VectorSize };
};

template<unsigned VectorSize>
void do_something(SizedDoubleVector<VectorSize>& point,
SizedDoubleVector<VectorSize>& vec)
{
for (int i = 0; i < SizedDoubleVector<VectorSize>::size; i++)
point *= vec;
}

int
main(int argc, char **argv)
{
SizedDoubleVector<3> three_point;
SizedDoubleVector<3> three_vec;

do_something(three_point, three_vec); // fine

SizedDoubleVector<4> four_vec;
do_something(three_point, four_vec); // compile error
}

Sean
 
K

Kai-Uwe Bux

I have an application where I need to do some computations on
vectors of numerical data. Specifically, the routine requires an
N-dimensional point and an N-dimensional vector as input.
[...]
It is no problem to do this at run-time and control parameters,
but this would imply a run-time overhead I would prefer
to avoid, if possible, and do all this book-keeping at
compile time instead.

How about something like this?

#include <vector>

using namespace std;

template<unsigned VectorSize>
class SizedDoubleVector : public vector<double>
{
public:
SizedDoubleVector() : vector<double>(VectorSize) {}
enum { size = VectorSize };
};
[snip]

Since you inherit publicly, methods like push_back() are still available.
Thus, you can change the length of the vector. Basically, the class is not
enforcing its invariant but requires flawless cooperation from the
programmer.

One could use private inheritance and make only those methods available that
do not change the length of the vector.


Best

Kai-Uwe Bux
 
V

Vidar Hasfjord

Hi all.

I have an application where I need to do some computations on
vectors of numerical data. Specifically, the routine requires an
N-dimensional point and an N-dimensional vector as input.

Check out std::valarray or a linear algebra library such as
Boost.uBLAS. The latter has vector types for both static vectors (size
known at compile-time) as well as dynamic vectors (size determined and
variable at run-time). Only dynamic vectors are supported by
std::valarray, and checks that operand sizes match are done at
runtime.

Regards,
Vidar Hasfjord
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top