The C++11 standard specifies that vwscanf is available via header <cwchar> (and therefore <wchar.h>). However it seems to be lacking in Visual C++. With that function I could write …
inline int scanf( CodingValue const* format, ... )
{
va_list args;
va_start( args, format );
return ::vwscanf( format->ptr(), args );
}
but without it, i.e. with Visual C++ 10.0, which also appears to lack support for C++11 variadic templates, I’m reduced to writing …
inline int scanf(
CodingValue const* format,
void* a01 = 0, void* a02 = 0, void* a03 = 0, void* a04 = 0, void* a05 = 0,
void* a06 = 0, void* a07 = 0, void* a08 = 0, void* a09 = 0, void* a10 = 0,
void* a11 = 0, void* a12 = 0
)
{
int const nArgs = !!a01 + !!a02 + !!a03 + !!a04 + !!a05 + !!a06 +
!!a07 + !!a08 + !!a09 + !!a10 + !!a11 + !!a12;
BasicCodingValue const* const f = format->ptr();
switch( nArgs )
{
case 0: return ::wscanf( f );
case 1: return ::wscanf( f,a01 );
case 2: return ::wscanf( f,a01,a02 );
case 3: return ::wscanf( f,a01,a02,a03 );
case 4: return ::wscanf( f,a01,a02,a03,a04 );
case 5: return ::wscanf( f,a01,a02,a03,a04,a05 );
case 6: return ::wscanf( f,a01,a02,a03,a04,a05,a06 );
case 7: return ::wscanf( f,a01,a02,a03,a04,a05,a06,a07 );
case 8: return ::wscanf( f,a01,a02,a03,a04,a05,a06,a07,a08 );
case 9: return ::wscanf( f,a01,a02,a03,a04,a05,a06,a07,a08,a09 );
case 10: return ::wscanf( f,a01,a02,a03,a04,a05,a06,a07,a08,a09,a10 );
case 11: return ::wscanf( f,a01,a02,a03,a04,a05,a06,a07,a08,a09,a10,a11 );
case 12: return ::wscanf( f,a01,a02,a03,a04,a05,a06,a07,a08,a09,a10,a11,a12 );
}
}
Or, I could do some assembly, as I did like 15 years ago when I encountered similar (or perhaps the same) problem, but that does not feel quite right.
Can you suggest some better way?
As I understand it’s not an error to call a variadic function with extra (i.e. more than asked for) arguments in C (and possibly in C++, although I’ve yet to locate a definitive statement either way on that).
The best I’ve got on the C++ side so far is 5.2.2 [expr.call]:
18.10 [support.runtime] defers to the ISO C 4.8.1.1 on the subject of restrictions for
va_start().I can’t see anywhere that says “you must not pass extra arguments”, so it seems that it wouldn’t be unreasonable to assume that it’s not prohibited.
If it’s not legal in C++ to give extra arguments then you can use C++ to handle the default arguments still and call a C function with the defaults having been “filled out” so that you only ever call the one C function, regardless of how many arguments were given.
So in C++ you’d do:
and then in C you could do: