473,903 Members | 5,218 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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,ar g2);

connect(arg1,ar g2,arg3);

connect(arg1,ar g2,arg3,arg4);

connect(arg1,ar g2,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 5993
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(arglis t, count);
//you can get the value of any arguement by
argX=va_arg(arg list,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(arglis t, count);
//you can get the value of any arguement by
argX=va_arg(arg list,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(arglis t, count);
//you can get the value of any arguement by
argX=va_arg(arg list,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******** *************@w agner.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
4921
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, Pujo
4
4686
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
3896
by: * ProteanThread * | last post by:
but depends upon the clique: http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&threadm=954drf%24oca%241%40agate.berkeley.edu&rnum=2&prev=/groups%3Fq%3D%2522cross%2Bposting%2Bversus%2Bmulti%2Bposting%2522%26ie%3DUTF-8%26oe%3DUTF-8%26hl%3Den ...
0
3798
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 and white CCITT4 compressed files (frames) inside the tiff. Every now and then I receive a mixture of black and white CCITT4 and JPEG compressed files, and sometimes just multi-page tiffs with JPEG only. The code runs great when dealing with the...
6
8195
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
17898
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 problem is the charset? How I can avoid this warning? But the worst thing isn't the warning, but that the program doesn't work! The program execute all other operations well, but it don't print the converted letters: for example, in the string...
5
6010
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 TIFF to several PNG files this causes a problem, becuase the resulting image is (the page to the far left and a lot of black space surrounding it and a filesize that is larger than needed. Any ideas?
5
5785
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 know a good place to start looking for some theory on the subject of multi user applications? I know only bits and pieces, like about transactions, but a compendium of possible approches to multi user programming would be very appreciated!
0
2336
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 Systems (MuCoCoS'08) Barcelona, Spain, March 4 - 7, 2008; in conjunction with CISIS'08. <http://www.par.univie.ac.at/~pllana/mucocos08> *********************************************************************** Context
1
9333
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 "PAR.UniqueID" could not be bound. The multi-part identifier "PAR.PAR_Status" could not be bound. The multi-part identifier "Salary.New_Salary" could not be bound. The multi-part identifier "Salary.UniqueID" could not be bound. The multi-part...
0
11283
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10875
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10501
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9685
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7206
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6093
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4727
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4308
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3324
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.