Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine if va_list is empty

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.

like image 680
BabelFish Avatar asked Jul 25 '12 15:07

BabelFish


People also ask

Can va_list be null?

Since va_list is a thingy, you can't assign a NULL. Basically va_list is a C++ class that has no constructors, no operator==, no operator void*, etc.

What does va_list mean?

va_list is a complete object type suitable for holding the information needed by the macros va_start, va_copy, va_arg, and va_end. If a va_list instance is created, passed to another function, and used via va_arg in that function, then any subsequent use in the calling function should be preceded by a call to va_end.


1 Answers

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.

like image 117
Mike Seymour Avatar answered Sep 18 '22 23:09

Mike Seymour