Topic: Proposal: variable number of arguments with specified type


Author: werner@bau2.uibk.ac.at (Werner Benger)
Date: 14 Jun 1994 18:50:14 GMT
Raw View
Hello everybody !
   I would like to propose an extension to the C++ syntax to allow
ntaining this number.
There should be no need to have something like a 'format string' or other
explicit parameter, because the compiler does know the parameter number
when parsing the source. Some 'extra information' given by the programmer
might be prone to errors.
  There are two ideas to give the function access to the parameter number.
First, the dimension of the pseudo array could contain the definition
of an 'int' parameter, which holds the information:

int sum(... int v[int n])
{
int r = 0;
 for(int i=0;i<n;i++)
  r += v[i];
 return i;
}

  Another idea is to use the 'sizeof' Operator with a dynamic meaning.
In this case, 'sizeof <pseudo array>' is not a constant number generated
at compile time, but an access to the implicit size parameter:

int sum(... int v[])
{
int n = sizeof(v)/sizeof(int);
 /* ... */
}

In either case the call of 'sum()' would be very simple:

int     a = sum(2,3),      // a = 2+3
 b = sum(7,8,9,10); // b = 7+8+9+10

   Of course there may be other parameters before and also after the
'fixed type ellipsis', if they can be distinguished from the ellipsis
type. It might be possible to have more than one 'fixed type ellipsis',
if the function call remains unambigous.
   Another advantage of the 'fixed type ellipsis' is that this is the only
possibility to pass a reference. With the C ellipsis, a structure can only
be passed by values or through an explicit pointer.


------------------ Alternatives in current C++ -----------------------------

For now, the best one can do is to overload the comma operator
to build a list of objects. It minimizes the calling code to
double parentheses:

class Object  { /* ... */};
class List_of_Objects
{
 /* ... */

 // construct a List from the first Object
 List_of_Objects(const Object&);

 Object& operator[](int); // for easy access
};

List_of_Objects operator,(List_of_Objects list, const Object&);

void f(List_of_Objects List);

For now, the best one can do is to overload the comma operator
to build a list of objects. It minimizes the calling code to
double parentheses:

class Object  { /* ... */};
class List_of_Objects
{
 /* ... */

 // construct a List from the first Object
 List_of_Objects(const Object&);

 Object& operator[](int); // for easy access
};

List_of_Objects operator,(List_of_Objects list, const Object&);

void f(List_of_Objects List);

class Object  { /* ... */};

void f(... Object& List[]);

main()
{
Objects a,b,c,d;

 f(a,b,c,d); // variable number of arguments
}


------------------------- Examples   ---------------------------------------

Below are some practical examples, where the new syntax could be of use:

* Imagine a 'class Tensor' and a 'class Index'. A Tensor might be
indiced with zero or more indices. The access function for Tensor
elements might be defined as:

 Element& Tensor::operator()(... Index i[]);

A Tensor can then be indiced by an arbitrary number of indices.


* Imagine a 'class Function' and a 'class Variable'. A Function may
depend on zero or more variables. The constructor might be defined as:

 Function::Function(... Variable dependencies[]);


* Imagine a function to draw a polygon, given some vertices. It could
easily be defined as:

 void drawpolygon(... const Vertex& Points[]);

----------------------------------------------------------------------------

Finally I would like to ask the readers of this newsgroup the following
questions:

* Are there any inconsistency problems or overlooked harmful consequences
  with the proposed syntax ?

* Is it portable to access parameters which are pushed on the stack in the
  same manner as an array access ?

* Why has it not already been implemented in ordinary C to have a possibility
  to get the number of ellipsis parameters ?

* What else reasons count against the implementation of a 'fixed type ellipsis'
  in C++ ?


Thank you for your interest and your response !