Jump to content
  • Advertisement
Sign in to follow this  
Harryu

C++ functions with variable number of arguments

This topic is 4061 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Sorry about the vague subject, but I didn't really know how to say it. Basically, you know how there are functions like printf, which can take different numbers of arguments, ranging from 2 to (I don't know how many). I was wondering how (if it's possible) could you create a function like this. Thanks, Harry

Share this post


Link to post
Share on other sites
Advertisement
Here are a couple of relevant links:

-->
-->

For more information, Google 'varargs', 'stdarg', and/or 'cstdarg'.

You might also consider telling us a) whether you're programming in C or C++, and b) why you need a variadic function. Depending on the nature of the problem, someone may be able to recommend a better solution than using C's variable argument list functionality.

Share this post


Link to post
Share on other sites
Short answer: you don't. These functions interact badly with types (and, in particular, are not type-safe). The preferred alternative is to use operator chaining to achieve the same effect, because:
  • It's easier to understand and use.
  • There are less pitfalls.
  • It's type-safe.
  • It handles pass-by-reference and temporary values correctly.
  • It's easily extensible.


See boost::format for an example.

Longer answer: variadic functions are a "feature" of the C standard library, parts of which were included in C++ for legacy reasons.

Share this post


Link to post
Share on other sites
Ok, thanks for both the answers and the links.

To answer the questions, a) C++, b) curiosity.

That's all I really needed, and thanks for telling me it's not good practice to use them before I went off to find some unnecessary use for them in a game or something!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!