Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Template that can take variable number of arguments

Reply
Thread Tools

Template that can take variable number of arguments

 
 
Ganny
Guest
Posts: n/a
 
      04-06-2005
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

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      04-06-2005
Ganny wrote:
> 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
 
Reply With Quote
 
 
 
 
ulrich
Guest
Posts: n/a
 
      04-06-2005
On 6 Apr 2005 06:24:25 -0700, Ganny <> wrote:

> 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()
 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      04-06-2005
ulrich wrote:
> On 6 Apr 2005 06:24:25 -0700, Ganny <> wrote:
>
>> 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.


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
 
Reply With Quote
 
??
Guest
Posts: n/a
 
      04-06-2005

"Ganny" <> ???????/???????? ? ???????? ?????????:
news: oups.com...
> 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)tr(&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)(78(777)();

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




 
Reply With Quote
 
Ioannis Vranos
Guest
Posts: n/a
 
      04-06-2005
Ganny wrote:

> 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:air 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.



--
Ioannis Vranos

http://www23.brinkster.com/noicys
 
Reply With Quote
 
Evan
Guest
Posts: n/a
 
      04-06-2005
> 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.

 
Reply With Quote
 
ulrich
Guest
Posts: n/a
 
      04-07-2005
On Wed, 06 Apr 2005 10:47:42 -0400, Victor Bazarov
<> wrote:

> ulrich wrote:
>> On 6 Apr 2005 06:24:25 -0700, Ganny <> wrote:
>>
>>> 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.

>
> From that point of view, a pair of iterators is much more preferable.


in order to get independent from the type of the container?
 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      04-07-2005
ulrich wrote:
> On Wed, 06 Apr 2005 10:47:42 -0400, Victor Bazarov
> <> wrote:
>
>> ulrich wrote:
>>
>>> On 6 Apr 2005 06:24:25 -0700, Ganny <> wrote:
>>>
>>>> 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.

>>
>>
>> From that point of view, a pair of iterators is much more preferable.

>
>
> in order to get independent from the type of the container?


Absolutely.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
problem in running a basic code in python 3.3.0 that includes HTML file Satabdi Mukherjee Python 1 04-04-2013 07:48 PM
Calling a function that expects variable arguments from a functionwith variable arguments Navaneeth C Programming 4 11-20-2010 05:35 AM
Call again a variadic function (... variable number of arguments)with same arguments that its variadic wrapper moreau.steve@gmail.com C Programming 3 12-31-2008 07:13 AM
Variable to take type of templated class with variable template parameter David Sanders C++ 4 10-30-2007 07:33 AM
template template arguments: expected a class template, got `Component<T1, T2, T3> gary.bernstein@gmail.com C++ 1 06-08-2007 07:10 AM



Advertisments