Topic: Type-Safe variable number of arguments


Author: g2devi@cdf.toronto.edu (Robert N. Deviasse)
Date: Sat, 23 Apr 1994 03:59:29 GMT
Raw View
In article <1994Apr20.065428.11588@sat.mot.com> hall_j@sat.mot.com (Joseph Hall) writes:
>In article <CoB7wC.HwE@discus.technion.ac.il> herbst@techunix.technion.ac.il (Herbst OMR) writes:
>>Is it possible to create a type-safe function taking variable number
>>of arguments *of the same type* ?
>
>How variable?  By that, I mean, would
>
>  #define NO_ARG -9999
>  int x(int arg1=NO_ARG, int arg2=NO_ARG, int arg3=NO_ARG)
>  {
>  }
>
>work for you?
>

Or you can define:
   int f(Varg<type*> args);
and use it as:
   f(Varg<type*>(&t1,&t2,&t3));

Just define Varg<T> as:

    template<class T>
       class Varg{
         public:
           Varg() : varg(0), nargs(0) {}
           Varg(T a1) : varg(new T[1]), nargs(1) { varg[0]=a1; }
           Varg(T a1,T a2) : varg(new T[2]), nargs(2) { varg[0]=a1; varg[1]=a2; }
           // ... Go as high as you think you'll ever need
           ~Varg()  { delete[] varg; }

           T* const varg;    // Could also use an array here
           const nargs;
       };

You need a little more specification in calling your function, but it's not
that bad, and you're given the ability to pass more than one variable length
list to a function.

>--
>Joseph Nathan Hall | Joseph's Law of Interface Design: Never give your users
>Software Architect | a choice between the easy way and the right way.
>Gorca Systems Inc. |                 joseph@joebloe.maple-shade.nj.us (home)
>(on assignment)    | (602) 732-2549 (work)  Joseph_Hall-SC052C@email.mot.com

Take care
    Robert

--
/----------------------------------+------------------------------------------\
| Robert N. Deviasse               |"If we have to re-invent the wheel,       |
| EMAIL: g2devi@cdf.utoronto.ca    |  can we at least make it round this time"|
+----------------------------------+------------------------------------------/




Author: barmar@think.com (Barry Margolin)
Date: 18 Apr 1994 16:11:22 GMT
Raw View
In article <CoB7wC.HwE@discus.technion.ac.il> herbst@techunix.technion.ac.il (Herbst OMR) writes:
>Is it possible to create a type-safe function taking variable number
>of arguments *of the same type* ?

No.  You could create a function that takes an array, though.  This would
be type-safe and still allow you to pass a variable number of values.
--
Barry Margolin
System Manager, Thinking Machines Corp.

barmar@think.com          {uunet,harvard}!think!barmar




Author: hall_j@sat.mot.com (Joseph Hall)
Date: Wed, 20 Apr 1994 06:54:28 GMT
Raw View
In article <CoB7wC.HwE@discus.technion.ac.il> herbst@techunix.technion.ac.il (Herbst OMR) writes:
>Is it possible to create a type-safe function taking variable number
>of arguments *of the same type* ?

How variable?  By that, I mean, would

  #define NO_ARG -9999
  int x(int arg1=NO_ARG, int arg2=NO_ARG, int arg3=NO_ARG)
  {
  }

work for you?

--
Joseph Nathan Hall | Joseph's Law of Interface Design: Never give your users
Software Architect | a choice between the easy way and the right way.
Gorca Systems Inc. |                 joseph@joebloe.maple-shade.nj.us (home)
(on assignment)    | (602) 732-2549 (work)  Joseph_Hall-SC052C@email.mot.com




Author: herbst@techunix.technion.ac.il (Herbst OMR)
Date: Fri, 15 Apr 1994 16:45:48 GMT
Raw View
Is it possible to create a type-safe function taking variable number
of arguments *of the same type* ?

Like:

f(int x,... ) but the compiler enforces all args to be ints.

Yaron





Author: pjl@graceland.att.com (Paul J. Lucas)
Date: Fri, 15 Apr 1994 23:00:52 GMT
Raw View
In <CoB7wC.HwE@discus.technion.ac.il> herbst@techunix.technion.ac.il (Herbst OMR) writes:

 In future, post such questions to comp.LANG.c++.

>Is it possible to create a type-safe function taking variable number
>of arguments *of the same type* ?

 No.
--
 - Paul J. Lucas
   AT&T Bell Laboratories
   Naperville, IL