Template that can take variable number of arguments

G

Ganny

Is it possible to write a template function min that takes variable
number of arguments? It should be without using ugly C var args,
arrays, or statically overloading the method for N arguments. Say, min
function which will take N arguments. Any approaches like generative
programming will help?

-Ganesh
 
V

Victor Bazarov

Ganny said:
Is it possible to write a template function min that takes variable
number of arguments? It should be without using ugly C var args,
arrays, or statically overloading the method for N arguments. Say, min
function which will take N arguments. Any approaches like generative
programming will help?

Just overload the min. The number of arguments is essentially one of
the type traits of the function.

V
 
U

ulrich

Is it possible to write a template function min that takes variable
number of arguments? It should be without using ugly C var args,
arrays, or statically overloading the method for N arguments. Say, min
function which will take N arguments. Any approaches like generative
programming will help?

if you insist on searching the minimum yourself, my suggestion would be to
pass a std::vector<> to your function. your function can then easily
iterate through the vector and search the minimum.

however, there is the class std::valarray<>, featuring the method - guess
what - std::valarray<>::min()
 
V

Victor Bazarov

ulrich said:
if you insist on searching the minimum yourself, my suggestion would be
to pass a std::vector<> to your function. your function can then
easily iterate through the vector and search the minimum.

From that point of view, a pair of iterators is much more preferable.
however, there is the class std::valarray<>, featuring the method -
guess what - std::valarray<>::min()

V
 
?

??

Ganny said:
Is it possible to write a template function min that takes variable
number of arguments? It should be without using ugly C var args,
arrays, or statically overloading the method for N arguments. Say, min
function which will take N arguments. Any approaches like generative
programming will help?

-Ganesh

Another suggestion would be to pass one at a
time,just like Andrei Alexandrescu suggests in
one of his articles,"Inline Containers".Just
imagine how standard streams handle this:
namespace manyargs{
template <typename T>
class minfunc
{
public:
minfunc(const T& x):ptr(&x){}

minfunc& operator() (const T& x)
{
if(*ptr_>x) ptr_=&x;
return *this;
}

const T& operator() ()
{
return *ptr_;
}
private:
const T* ptr_;
};

template <typename T>
minfunc<T> min(const T& x)
{
return minfunc<T>(x);
}
}
And use it like this:

int a=manyargs::min(123)(56)(788)(777)();

I agree that the syntax is somewhat ugly,but
still completely typesafe, as opposed to printf
and their kin.
 
I

Ioannis Vranos

Ganny said:
Is it possible to write a template function min that takes variable
number of arguments? It should be without using ugly C var args,
arrays, or statically overloading the method for N arguments. Say, min
function which will take N arguments. Any approaches like generative
programming will help?


You could pass in it a std::pair of pairs. However I think Boost provides more elegant
solutions:

http://www.boost.org


Just download and set it up (it is easy) for your compiler.
 
E

Evan

if you insist on searching the minimum yourself, my suggestion would
be to
pass a std::vector<> to your function. your function can then easily
iterate through the vector and search the minimum.

But then you have to put stuff in an array. I think he's looking for
something you could call like:

min(x, y, z, 10, k, g.getValue());


The answer to this question is yes and no. I would suggest, as victor
did, writing just a series of overloaded functions min(a,b),
min(a,b,c), etc., possibly with each function referring to the last.
For instance:

template<typename T>
T min(T a, T b, T c, T d)
{
T maybeMin = min(a,b,c);

if(maybeMin < d)
return maybeMin;
else
return d;
}

Make it up to 8 arguments or whatever you need.

There was a proposal before the C++ standards committee for type-safe,
variable length function parameters to replace the '...' in functions
such as printf. If accepted, this would provide a nicer way of doing
this. However, it won't be for a while; it'd need to be accepted, added
to the next version of the standard in who-knows-when, then implemented
before you could use it, so it's still a number of years off.
 

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,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top