473,387 Members | 1,899 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Template that can take variable number of arguments

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

Jul 23 '05 #1
7 3324
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
Jul 23 '05 #2
On 6 Apr 2005 06:24:25 -0700, Ganny <sg******@gmail.com> 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()
Jul 23 '05 #3
ulrich wrote:
On 6 Apr 2005 06:24:25 -0700, Ganny <sg******@gmail.com> 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
Jul 23 '05 #4
??

"Ganny" <sg******@gmail.com> ???????/???????? ? ???????? ?????????:
news:11**********************@z14g2000cwz.googlegr 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):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.


Jul 23 '05 #5
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::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.

--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 23 '05 #6
> 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.

Jul 23 '05 #7
ulrich wrote:
On Wed, 06 Apr 2005 10:47:42 -0400, Victor Bazarov
<v.********@comAcast.net> wrote:
ulrich wrote:
On 6 Apr 2005 06:24:25 -0700, Ganny <sg******@gmail.com> 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.
Jul 23 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Bart Samwel | last post by:
Hi everybody, I would really like some help explaining this apparent discrepancy, because I really don't get it. Here is the snippet: void foo(int&); void foo(int const&); ...
10
by: Clay_Culver | last post by:
I have heard that it is possible to use classes + template magic to make standalone functions (or maybe just a functor which acts like a function) which can accept variable length arguments without...
6
by: Stuart McGraw | last post by:
I am looking for a VBA "format" or "template" function, that is, a function that takes a format string and a varying number of arguments, and substitutes the argument values into the format string...
3
by: Serge Skorokhodov (216716244) | last post by:
Hi, I just seeking advice. Some background information first because I've run into issues that seems pretty obscure to me:( Quick search through KB yields next to nothing. Simplified samples...
3
by: Tomás | last post by:
Let's say we have a variable length argument list function like so: unsigned GetAverage(unsigned a, unsigned b, ...); I wonder does the compiler go through the code looking for invocations of...
9
by: jc | last post by:
Hi all, I have a data type to use that I can't modify its codes. E.g. This template data type is data_type. When I declare a variable of this data_type, I write as following: data_type(8,...
11
by: Fan Yang | last post by:
I'm reading Modern C++ Design, and it is saying "Variable template parameters simply don't exist." But I find VC7.1 & VC8 support this feature.Who can tell me that which is right -_-b Many thanks.
2
by: ndbecker2 | last post by:
On upgrading from gcc-4.1.2 to gcc-4.3, this (stripped down) code is now rejected: #include <vector> #include <iostream> template<typename T, template <typename Aclass CONT=std::vector>...
8
by: flopbucket | last post by:
Hi, I want to provide a specialization of a class for any type T that is a std::map. template<typename T> class Foo { // ... };
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.