473,407 Members | 2,598 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,407 software developers and data experts.

Arbitrary number of arguments

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.
--
(Should insert humorous quotation here)
Sep 14 '05 #1
4 3113
* 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).

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Sep 14 '05 #2
Magnus Jonneryd wrote:
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....becab933500acc

Cheers! --M

Sep 14 '05 #3
Magnus Jonneryd wrote:
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
Sep 14 '05 #4
Thanks all.

--
(Should insert humorous quotation here)
Sep 15 '05 #5

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

Similar topics

4
by: rbt | last post by:
How do I set up a function so that it can take an arbitrary number of arguments? For example, I have a bunch of expenses which may grow or shrink depending on the client's circumstance and a...
5
by: Honestmath | last post by:
Hello everyone, I'm using a root finding algorithm in a function that only takes as arguments a function pointer and two variables that represent guesses at the roots of the function. int...
8
by: Steve Neill | last post by:
Can anyone suggest how to create an arbitrary object at runtime WITHOUT using the deprecated eval() function. The eval() method works ok (see below), but is not ideal. function Client() { }...
3
by: Randy Yates | last post by:
Hi, We know we can build arrays of variables of the same type and arrays of functions of the same "type" (i.e., same return value and same parameters), but is there a way to automate the calling...
12
by: Chadwick Boggs | last post by:
I need to perform modulo operations on extremely large numbers. The % operator is giving me number out of range errors and the mod(x, y) function simply seems to return the wrong results. Also,...
135
by: Xah Lee | last post by:
Tabs versus Spaces in Source Code Xah Lee, 2006-05-13 In coding a computer program, there's often the choices of tabs or spaces for code indentation. There is a large amount of confusion about...
2
by: waylonflinn | last post by:
I'm looking for a way to invoke methods with an arbitrary number of parameters of arbitrary type from within a single method, when those parameters are known at the time of invocation of the...
2
by: John O'Hagan | last post by:
Hi Pythonistas, I'm looking for the best way to pass an arbitrary number and type of variables created by one function to another. They can't be global because they may have different values...
0
by: John O'Hagan | last post by:
On Tue Sep 30 11:32:41 CEST 2008, Steven D'Aprano Thanks, both to you and Bruno for pointing this out, I'll certainly be using it in future.
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: 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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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...
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...
0
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,...

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.