Alex Rivera | Logout

How to determine if va_list is empty

Asked 2012-07-25T15:22:47.153
8

I have been reading that some compilers support va_list with macros and users were able to overload the functionality with other macros in order to count the va_list.

With visual studio, is there a way to determine if the va_list is empty (aka count==0)? Basically I would like to know this condition:

extern void Foo(const char* psz, ...);
void Test()
{
  Foo("My String"); // No params were passed
}

My initial thought was to do something like this:

va_list vaStart;
va_list vaEnd;
va_start(vaStart, psz);
va_end(vaEnd);
if (vaStart == vaEnd) ...

The problem is that va_end only sets the param to null.

#define _crt_va_start(ap,v)  ( ap = (va_list)_ADDRESSOF(v) + _INTSIZEOF(v) )
#define _crt_va_arg(ap,t)    ( *(t *)((ap += _INTSIZEOF(t)) - _INTSIZEOF(t)) )
#define _crt_va_end(ap)      ( ap = (va_list)0 )

I was thinking of maybe incorporating a terminator but I would want it to be hidden from the caller so that existing code doesnt need to be changed.

Edit
Report

1 Answer

8

There is no way to tell how many arguments are passed through ..., nor what type they are. Variadic function parameters can only be used if you have some other way (e.g. a printf-style format string) to tell the function what to expect; and even then there is no way to validate the arguments.

C++11 provides type-safe variadic templates. I don't know whether your compiler supports these, or whether they would be appropriate for your problem.

answered 2012-07-25T15:42:52.940

Your Answer