Sign in to follow this  
AndreiVictor

C++ : How do you pass the ... arguments to another function?

Recommended Posts

Hi Guys! I am really new to variable argument lists but have done some functions that use it. I just want to ask how to do this simple problem. Let's say for this exercise, I want to wrap the default snprintf of the API to a function called nSnprintf. How do you pass the contents of the ellipses to the real _snprintf function? I tried this and didn't work out:
int Kernel::nSnprintf( char * p_szBuffer, unsigned int p_nCount,
    const char * p_cszFormat, ... )
{
    va_list va_args;
    return _snprintf( p_szBuffer, p_nCount, p_cszFormat, va_args );
}

Share this post


Link to post
Share on other sites
There's a way involving certain functions specially designed to take va_lists, but you shouldn't be taking advantage of it. This is C++. Variable argument lists are only around for compatibility with C, and there's a raft of good reasons not to write any new code with them, the primary one being that type safety is not preserved. I know they seem really awesome and convenient and flexible, but there are just too many gotchas.

Share this post


Link to post
Share on other sites
As Sneftel said you shouldn't be using varargs in c++, you should use operator chaining instead, if the reason your using it is for format strings (i.e. you dont like iostreams and operator<<) then have a look at boost.format

Share this post


Link to post
Share on other sites
I usually find it easiest to do the string expansion/formatting manually, then pass that on to printf. Make sure you don't just pass it straight to printf though, you have to do:
printf("%s", manuallyFormattedString);

Otherwise stray control characters in the manually formatted string can make printf think it needs to do additional formatting of it's own.

Share this post


Link to post
Share on other sites
Quote:
Original post by AndreiVictor
Hi Guys! I am really new to variable argument lists but have done some functions that use it. I just want to ask how to do this simple problem. Let's say for this exercise, I want to wrap the default snprintf of the API to a function called nSnprintf. How do you pass the contents of the ellipses to the real _snprintf function?

I tried this and didn't work out:


int Kernel::nSnprintf( char * p_szBuffer, unsigned int p_nCount,
const char * p_cszFormat, ... )
{
va_list va_args;
return _snprintf( p_szBuffer, p_nCount, p_cszFormat, va_args );
}


Setting aside what Sneftel and Julian90 said about not using varargs in C++, and fleshing out further what grelle wrote...

The va_list variable should be initialized and deinitialized.



#include <stdarg.h>

int Kernel::nSnprintf( char * p_szBuffer, unsigned int p_nCount,
const char * p_cszFormat, ... )
{
int retval = 0;
va_list va_args;
va_start( va_args, p_cszFormat );
retval = _vsnprintf( p_szBuffer, p_nCount, p_cszFormat, va_args );
va_end(va_args);
return retval;
}



Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this