473,395 Members | 1,335 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

multi-arguments function/macro

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
Jul 22 '05 #1
7 5965
A. Saksena wrote:
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
Jul 22 '05 #2
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
......
.....
}

Jul 22 '05 #3
Sagar Choudhary wrote:
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
Jul 22 '05 #4
relax man
I just wrote to give an idea

Jul 22 '05 #5
Sagar Choudhary wrote:
Sagar Choudhary wrote:
Please, quote the post you are answering to.
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?

relax man
?
I just wrote to give an idea


Next time, write some valid and compilable code. That will be of more help.
Jonathan
Jul 22 '05 #6
Does it also work for user defined data types and classes
Abhishek

"Jonathan Mcdougall" <jo***************@DELyahoo.ca> wrote in message
news:ID*********************@wagner.videotron.net. ..
A. Saksena wrote:
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

Jul 22 '05 #7
A. Saksena wrote:
"Jonathan Mcdougall" <jo***************@DELyahoo.ca> wrote in message
A. Saksena wrote:
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?


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
Jul 22 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

37
by: ajikoe | last post by:
Hello, Is anyone has experiance in running python code to run multi thread parallel in multi processor. Is it possible ? Can python manage which cpu shoud do every thread? Sincerely Yours,...
4
by: Frank Jona | last post by:
Intellisense with C# and a multi-file assembly is not working. With VB.NET it is working. Is there a fix availible? We're using VisualStudio 2003 Regards Frank
12
by: * ProteanThread * | last post by:
but depends upon the clique: ...
0
by: frankenberry | last post by:
I have multi-page tiff files. I need to extract individual frames from the multi-page tiffs and save them as single-page tiffs. 95% of the time I receive multi-page tiffs containing 1 or more black...
6
by: cody | last post by:
What are multi file assemblies good for? What are the advantages of using multiple assemblies (A.DLL+B.DLL) vs. a single multi file assembly (A.DLL+A.NETMODULE)?
4
by: mimmo | last post by:
Hi! I should convert the accented letters of a string in the correspondent letters not accented. But when I compile with -Wall it give me: warning: multi-character character constant Do the...
5
by: Shane Story | last post by:
I can seem to get the dimensions of a frame in a multiframe tiff. After selecting activeframe, the Width/Height is still really much larger than the page's actual dimensions. When I split a...
5
by: bobwansink | last post by:
Hi, I'm relatively new to programming and I would like to create a C++ multi user program. It's for a project for school. This means I will have to write a paper about the theory too. Does anyone...
0
by: Sabri.Pllana | last post by:
We apologize if you receive multiple copies of this call for papers. *********************************************************************** 2008 International Workshop on Multi-Core Computing...
1
by: mknoll217 | last post by:
I am recieving this error from my code: The multi-part identifier "PAR.UniqueID" could not be bound. The multi-part identifier "Salary.UniqueID" could not be bound. The multi-part identifier...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.