multi-arguments function/macro

A

A. Saksena

Hi all,

Is it possible to write a function or a macro in C++, which is capable of
accepting any number of arguments.



To give an example, the following should be possible: -

connect(arg1,arg2);

connect(arg1,arg2,arg3);

connect(arg1,arg2,arg3,arg4);

connect(arg1,arg2,arg3,arg5,.);



I don't mind even if the upper limit to number of argument is fixed. But
definately I don't want overloaded functions.





Abhishek
 
J

Jonathan Mcdougall

A. Saksena said:
Hi all,

Is it possible to write a function or a macro in C++, which is capable of
accepting any number of arguments.

I don't mind even if the upper limit to number of argument is fixed. But
definately I don't want overloaded functions.

With a macro, no, except by giving them names like MACRO_1, MACRO_2,
etc. With functions, you have two choices: default arguments such as

void f(int a=1, int b=2, int c=3);

or printf-like functions

# include <cstdarg>
# include <iostream>

void f(int n ...)
{
va_list vl;
va_start(vl, n);

while (n-- > 0)
std::cout << va_arg(vl, int);

va_end(vl);
}

int main()
{
f(3, 1, 2, 3);
}

Output :
123

You should read about these and why they are *not* recommended. What
are you tring to achieve?


Jonathan
 
S

Sagar Choudhary

you can use va_list
define the function as connect (count,...);
where count may be the the number of args
in the function
connect ( count, ...)
{
va_list arglist;
va_start(arglist, count);
//you can get the value of any arguement by
argX=va_arg(arglist,type);
// the function will return the first optional arg of type type
......
.....
}
 
J

Jonathan Mcdougall

Sagar said:
you can use va_list
define the function as connect (count,...);
where count may be the the number of args
in the function
connect ( count, ...)
{
va_list arglist;
va_start(arglist, count);
//you can get the value of any arguement by
argX=va_arg(arglist,type);
// the function will return the first optional arg of type type

What about va_end() ?
......
.....
}

In what language is this code written in?


Jonathan
 
J

Jonathan Mcdougall

A. Saksena said:
Does it also work for user defined data types and classes

Please don't top-post. Rearranged.

If you are talking about variable-argument function (also called
variadic functions), yes it does, but you need to understand why.

When you don't specify the exact count of argument, you can obviously
not specify their type. Take my example again:

void f(int n ...)
{
// ...
}

int main()
{
f(3,1,2,3);
}

In this case, since you did not specify the all the arguments for f(),
the compiler cannot check if the types correspond. That means

void f(int n, ...)
{
va_list vl;
va_start(vl, n);

int my_int = va_arg(vl, int); // we are assuming its an int

std::cout << my_int;

va_end(vl);
}

int main()
{
double d = 10.5;

f(1, d); // we're passing a double
}

will compile with no error. That does not mean there are none. As you
see in f(), I converted the first argument of the list to 'int', but the
actual type I passed was a double. Problem.


class C
{
// ..
};

int main()
{
C c;
f(1, c); // ouch!!
}

The thing is, f() has *absolutely* no ways of knowing what type of
argument it received. That's why we usually use the first argument as a
string to be able to specify what are the remaining arguments.

printf("%s %d", "test", 10);

By the way, printf() looks like

int printf(const char* ...);

In this case, printf() will know, by looking at the string, that the
types of the arguments are char* and int because of the tokens %s and
%d. Beware if you did not pass the correct arguments.

So variadic functions are not for the faint of heart. It is extremely
easy no to give correct arguments to such a function. There are other
ways to do that. Explore them.

As I said before, default arguments might be of help or even a class
with a collection inside (or a plain collection such as a std::vector).

For us to be able to help you more, you should explain what you are
trying to do.


Jonathan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,241
Members
46,831
Latest member
RusselWill

Latest Threads

Top