Arbitrary number of arguments

M

Magnus Jonneryd

I'm trying to figure out if it's possible to implement a function in C++
that has an argument list of arbitrary length. Is this possible (I don't
want to use a data structure to accomplish this)?

I found a header stdarg.h that addresses this, but I also found an article
that stated that it shouldn't be used in C++ since it wasn't typesafe. Is
there any truth to the article or can i go a head and use the macros in
stdarg.h?

Grateful for all help.
 
A

Alf P. Steinbach

* Magnus Jonneryd:
I'm trying to figure out if it's possible to implement a function in C++
that has an argument list of arbitrary length. Is this possible
Yes.


(I don't
want to use a data structure to accomplish this)?

All solutions involve data structures, because the arguments are data.

But you may perhaps consider a solution à la std::cout's operator<< to be less
of a data structure than a dynamic call structure.

I found a header stdarg.h that addresses this, but I also found an article
that stated that it shouldn't be used in C++ since it wasn't typesafe. Is
there any truth to the article
Yes.


or can i go a head and use the macros in stdarg.h?

Yes again (but don't complain about the results).
 
M

mlimber

Magnus said:
I'm trying to figure out if it's possible to implement a function in C++
that has an argument list of arbitrary length. Is this possible (I don't
want to use a data structure to accomplish this)?

I found a header stdarg.h that addresses this, but I also found an article
that stated that it shouldn't be used in C++ since it wasn't typesafe. Is
there any truth to the article or can i go a head and use the macros in
stdarg.h?

stdarg.h uses data structures, too, as Alf mentions, and its use is
discouraged in C++. Likewise, cin/cout are typesafe, scanf/printf are
not, and the former are preferred in C++.

There are other approaches to a variable number of arguments if you
don't mind strange looking syntax; see this recent thread, especially
the post by Cy Edmunds:

http://groups.google.com/group/comp...90b30/5fbecab933500acc?hl=en#5fbecab933500acc

Cheers! --M
 
D

Default User

Magnus said:
I'm trying to figure out if it's possible to implement a function in
C++ that has an argument list of arbitrary length. Is this possible
(I don't want to use a data structure to accomplish this)?


You would be better off describing the actual problem you are trying to
solve, rather than your proposed solution (as it isn't a good one). We
may be able to give you a solution that doesn't require a function
taking an arbitrary number of arguments.




Brian
 

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
474,432
Messages
2,571,681
Members
48,796
Latest member
Greg L.

Latest Threads

Top