Connecting Tech Pros Worldwide Forums | Help | Site Map

multi-arguments function/macro

A. Saksena
Guest
 
Posts: n/a
#1: Jul 23 '05
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



Jonathan Mcdougall
Guest
 
Posts: n/a
#2: Jul 23 '05

re: multi-arguments function/macro


A. Saksena wrote:[color=blue]
> 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.[/color]

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
Sagar Choudhary
Guest
 
Posts: n/a
#3: Jul 23 '05

re: multi-arguments function/macro


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
......
.....
}

Jonathan Mcdougall
Guest
 
Posts: n/a
#4: Jul 23 '05

re: multi-arguments function/macro


Sagar Choudhary wrote:[color=blue]
> 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[/color]

What about va_end() ?
[color=blue]
> ......
> .....
> }[/color]

In what language is this code written in?


Jonathan
Sagar Choudhary
Guest
 
Posts: n/a
#5: Jul 23 '05

re: multi-arguments function/macro


relax man
I just wrote to give an idea

Jonathan Mcdougall
Guest
 
Posts: n/a
#6: Jul 23 '05

re: multi-arguments function/macro


Sagar Choudhary wrote:[color=blue]
> Sagar Choudhary wrote:[/color]

Please, quote the post you are answering to.
[color=blue][color=green][color=darkred]
>>> 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[/color][/color]
>
>
> What about va_end() ?
>
>[color=green][color=darkred]
>>> ......
>>> .....
>>> }[/color][/color]
>
>
> In what language is this code written in?[/color]
[color=blue]
> relax man[/color]

?
[color=blue]
> I just wrote to give an idea[/color]

Next time, write some valid and compilable code. That will be of more help.


Jonathan
A. Saksena
Guest
 
Posts: n/a
#7: Jul 23 '05

re: multi-arguments function/macro


Does it also work for user defined data types and classes
Abhishek

"Jonathan Mcdougall" <jonathanmcdougall@DELyahoo.ca> wrote in message
news:IDayd.55618$Y05.2631006@wagner.videotron.net. ..[color=blue]
> A. Saksena wrote:[color=green]
> > Hi all,
> >
> > Is it possible to write a function or a macro in C++, which is capable[/color][/color]
of[color=blue][color=green]
> > 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.[/color]
>
> 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[/color]


Jonathan Mcdougall
Guest
 
Posts: n/a
#8: Jul 23 '05

re: multi-arguments function/macro


A. Saksena wrote:[color=blue]
> "Jonathan Mcdougall" <jonathanmcdougall@DELyahoo.ca> wrote in message[color=green]
>> A. Saksena wrote:
>>[color=darkred]
>>>Hi all,
>>>
>>>Is it possible to write a function or a macro in C++, which is capable[/color][/color]
>
> of
>[color=green][color=darkred]
>>>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.[/color]
>>
>>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?[/color]
>
> Does it also work for user defined data types and classes[/color]

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
Closed Thread