extensible math functions: variable number of arguments and class templates

A

Alan

I have a couple of questions about using a variable number of
arguments in a function call (...). The context is that I have some
mathematical functions I created. I currently pass them a pair of
numbers (doubles), and the functions only work in one dimension. I
would ideally like to extend them to work in N dimensions, which means
I want to pass some undefined number of pairs of arguments. I will
loop through the N arguments to do the math in N dimensions.

1. Some consider using a variable number of arguments unsafe, due to
type checking concerns. So, is there a another but simple way to
extend the function so I can pass it N pairs of numbers? Or would you
simply recommend heavy error checking in the functions?

2. Can the arguments in a variable number of arguments call to a
function be class templates? As in:

template <class T>
T do_math (class T, . . .)
{
. . .
}

Thanks in advance,

Alan
 
A

Alf P. Steinbach

* Alan:
I have a couple of questions about using a variable number of
arguments in a function call (...). The context is that I have some
mathematical functions I created. I currently pass them a pair of
numbers (doubles), and the functions only work in one dimension. I
would ideally like to extend them to work in N dimensions, which means
I want to pass some undefined number of pairs of arguments. I will
loop through the N arguments to do the math in N dimensions.

Pass a vector of pairs.

1. Some consider using a variable number of arguments unsafe, due to
type checking concerns. So, is there a another but simple way to
extend the function so I can pass it N pairs of numbers? Or would you
simply recommend heavy error checking in the functions?

Pass a vector of pairs.

2. Can the arguments in a variable number of arguments call to a
function be class templates? As in:

template <class T>
T do_math (class T, . . .)
{
. . .
}

Passing (non-POD) class instances yields Undefined Behavior.
 
T

terminator

Pass a vector of pairs.
or pass a pair of vectors:

#include <vector>

void do_math(const std::vector<double>& ,const std::vector<double>&);

void myfunc(){
std::vector<double> v1,v2;
v1[1]=someValue1;
v1[2]=someValue2;
//and so forth
...
do_math(v1,v2);
}


regards,
FM.
 
A

Alf P. Steinbach

* terminator:
Pass a vector of pairs.
or pass a pair of vectors:

#include <vector>

void do_math(const std::vector<double>& ,const std::vector<double>&);

void myfunc(){
std::vector<double> v1,v2;
v1[1]=someValue1;
v1[2]=someValue2;
//and so forth
...
do_math(v1,v2);
}

Perhaps you can figure out why I did not recommend that?
 
A

Alf P. Steinbach

* terminator:
* terminator:
Pass a vector of pairs.
or pass a pair of vectors:
#include <vector>
void do_math(const std::vector<double>& ,const std::vector<double>&);
void myfunc(){
std::vector<double> v1,v2;
v1[1]=someValue1;
v1[2]=someValue2;
//and so forth
...
do_math(v1,v2);
}
Perhaps you can figure out why I did not recommend that?

Please clarify your point ; I am not that sharp.

A vector of pairs guarantees that there will be pairs of values, only.
A pair of vectors offers no such guarantee: the vector sizes can be
different. This introduces an unnecessary point of failure and error
detection (with attendant complexity) in the 'do_math' function.

(Btw., I think you meant 'push_back', not assignment, above. Or else
you meant to initialize the vectors with some size. As it stands the
above code is undefined behavior.)

Cheers,

- Alf
 
T

terminator

* terminator:




* terminator:
Pass a vector of pairs.
or pass a pair of vectors:
#include <vector>
void do_math(const std::vector<double>& ,const std::vector<double>&);
void myfunc(){
std::vector<double> v1,v2;
v1[1]=someValue1;
v1[2]=someValue2;
//and so forth
...
do_math(v1,v2);
}
Perhaps you can figure out why I did not recommend that?
Please clarify your point ; I am not that sharp.

A vector of pairs guarantees that there will be pairs of values, only.
A pair of vectors offers no such guarantee: the vector sizes can be
different. This introduces an unnecessary point of failure and error
detection (with attendant complexity) in the 'do_math' function.

(Btw., I think you meant 'push_back', not assignment, above. Or else
you meant to initialize the vectors with some size. As it stands the
above code is undefined behavior.)

Cheers,

- Alf

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?- Hide quoted text -

- Show quoted text -

ok, I can define my own number parametered template for vector- either
or not deriving from std version .

thanks for push_back
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top