Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

variable argument functions

0 views
Skip to first unread message

Shaleh

unread,
Oct 2, 1996, 3:00:00 AM10/2/96
to

How do I create variable argument functions in C++. I know how to do
this in C -- is there a difference in C++?? Any new twists?? Say I
want to create an add_em function with a prototype of int add_em( int
num_ints, ...) that returns the sum of the integers passed to it. I
could then sum 2 or 20 ints by passing them, (just an example not my
real intent ). Thank you in advance.

[ Send an empty e-mail to c++-...@netlab.cs.rpi.edu for info ]
[ about comp.lang.c++.moderated. First time posters: do this! ]

Kevin J Hopps

unread,
Oct 3, 1996, 3:00:00 AM10/3/96
to

Shaleh (no...@nowhere.net) wrote:
> How do I create variable argument functions in C++. I know how to do
> this in C -- is there a difference in C++?? Any new twists?? Say I
> want to create an add_em function with a prototype of int add_em( int
> num_ints, ...) that returns the sum of the integers passed to it. I
> could then sum 2 or 20 ints by passing them, (just an example not my
> real intent ). Thank you in advance.

Here's an example:
--
#include <iostream.h>
#include <stdarg.h>

int sum(int numInts, ...);

int main()
{
cout << sum(5, 1, 2, 3, 4, 5) << endl;
return 0;
}

int sum(int numInts, ...)
{
int result = 0;

va_list args;
va_start(args, numInts);
for (int i = 0; i < numInts; i++)
result += va_arg(args, int);
va_end(args);

return result;
}
--
Kevin J. Hopps, Imation kjh...@mmm.com
My opinions are my own. I speak neither for Imation nor 3M.

Herb Sutter

unread,
Oct 3, 1996, 3:00:00 AM10/3/96
to

On 2 Oct 1996 08:00:24 -0400, no...@nowhere.net (Shaleh) wrote:
>How do I create variable argument functions in C++. I know how to do
>this in C -- is there a difference in C++?? Any new twists?? Say I
>want to create an add_em function with a prototype of int add_em( int
>num_ints, ...) that returns the sum of the integers passed to it. I
>could then sum 2 or 20 ints by passing them, (just an example not my
>real intent ). Thank you in advance.

I just approved another reply showing how to use normal C-style
varargs. You can certainly do that in C++, but varargs are fragile,
error-prone, and IMO extremely dangerous.

One type-safe 'C++ way' is to pass a list<args> and let the function
deal with that. The sample program below shows another possible
type-safe way to implement variable arguments using helper classes.(*)
This example is meant to illustrate your question above, and so every
argument is of the same type and no history of arguments is kept; you
could just as easily build a list of all the arguments and have
different types at different positions (possibly dependent on other
types before them) by doing the appropriate work in the helper class'
operator(T)'s and any final work in the helper's operator().

(*) Instead of 'func(1,2,3)' the caller's syntax is 'func(1)(2)(3)'.
Note that this might actually be nice when each element is, say, a
coordinate position rather than just an int, since the caller gets to
write something like: 'path(0,1)(2,3)(4,5)' etc.

Personally I'm always reluctant to use any variable-arg construct, but
once in a while I suppose there's no easy way around it and you'll end
up doing something like the above or the below.

---8<-----------------------------------------------------------------
#include <iostream.h>
#include <typeinfo.h>

template<class T>
class SumHelper
{
public:
SumHelper(T t) : value(t) { };
SumHelper operator()(T t) { return SumHelper<T>(t+value); };
operator T() { cout << typeid(T).name() << " ";
return value; };
private:
T value;
};

template<class T>
SumHelper<T> sum(T t)
{
return SumHelper<T>(t);
}

int main()
{
cout << sum(1)(2)(3)(4)(5) << endl;
cout << sum(1.0)(2)(3)(4)(5) << endl;
return 0;
}
---
Herb Sutter (he...@cntc.com)

Current Network Technologies Corp.
3100 Ridgeway, Suite 42, Mississauga ON Canada L5L 5M5
Tel 416-805-9088 Fax 905-608-2611

J. Kanze

unread,
Oct 3, 1996, 3:00:00 AM10/3/96
to

no...@nowhere.net (Shaleh) writes:

> How do I create variable argument functions in C++. I know how to do
> this in C -- is there a difference in C++?? Any new twists?? Say I
> want to create an add_em function with a prototype of int add_em( int
> num_ints, ...) that returns the sum of the integers passed to it. I
> could then sum 2 or 20 ints by passing them, (just an example not my
> real intent ). Thank you in advance.

If all of the arguments have the same type, you could use a collector,
e.g.:

template< class T >
struct Collector : public list< T >
{
Collector& with( T const& obj )
{
push_back( obj ) ;
return *this ;
}
} ;

To take your example, you could then write:

int
sum( list< int > const& l )
{
int total = 0 ;
for ( list< int >::const_iterator i = l.begin() ;
i != l.end() ;
++ i )
total += *i ;
return total ;
}

This could be called with:

i = sum( Collector< int >().with( 2 )
.with( 5 )
.with( 3 ) ) ;

--
James Kanze (+33) 88 14 49 00 email: ka...@gabi-soft.fr
GABI Software, Sarl., 8 rue des Francs Bourgeois, 67000 Strasbourg, France
Conseils en informatique industrielle --
-- Beratung in industrieller Datenverarbeitung

0 new messages