Skip to main content
gamedev.net
🔒 Locked

variable argument list

Started by paic Dec 9, 2005 at 8:47 AM 2 replies 500+ views
Original Post
paic
paic
Hi, I wanted to know if there was an equivalent with the STL. For example, instead of include stdio.h and stdlib.h, and using fopen() and fprintf(), I now include fstream, and use ofstream and operator <<. Is there an equivalent with variable argument list, or should I still use stdarg.h, va_list, va_start, etc. ??
snk_kid
snk_kid
I don't quite understand your question, what is it going to be used for?

C variable argument lists can only be used on POD-types in C++, there are various methods of simluating NON-POD type compatible variadic functions:


  • Do as the iostreams do, overload some binary operator, make it a cascading function, if it makes sense or possible do buffering to increase preformance/efficiency


  • if buffering does not make sense or possible, then another alternative very similar to the previous one is using expression templates or there younger little brother Composition/Compositor objects (a term coined by Bjarne Stroustrup).


  • Use boost.preprocessor and generate N overloadeds for a particular template function where N can be user-configurable.

paic
paic
thx for the reply.
Guess I wasn't clear ^^ I wanted to know if I can replace that kind of code :
void aFunction(char *text, ...){   char     *msg;   int      len;   va_list  ap;   va_start(ap, text);   len = _vscprintf(text, ap);   msg = new char [len + 1];   _vsnprintf(msg, len, text, ap);   va_end(ap);   msg[len] = '\0';}

by something like
void aFunction(char *text, ...){   std::string  msg;   msg << text;   // or something like :   std::va_arg args(text);   args.format();   msg << args;}

In fact, I'm just looking for a simpler / safer way of handling variable argument lists. As std::string now replace all the char *, strcpy, etc. in my code, I wanted to find something to replace all my va_list, etc. ^^
snk_kid
snk_kid
Quote:
Original post by paic
I'm just looking for a simpler


The simplest way is to use string streams for that, there just like file streams and any stream type except there for strings in memory of-course [wink]. There are in header sstream and if you are still hampering for format strings well that is still possible with C++ iostreams check here.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.