I have a Visual Studio 2008 C++ DLL that exports a function that accepts a variadic parameter like this:
__declspec( dllexport ) void DLL_Foo( int count, ... )
{
va_list list;
va_start( list, count );
for( ; count; --count )
{
const wchar_t* item = va_arg( list, const wchar_t* );
if( count % 2 == 0 )
printfW( L"[%s] ", item );
else
printfW( L"%s ", item );
}
va_end( list );
printfW( L"\r\n" );
}
The expected usage is something like this:
DLL_Foo( 4, L"int", L"1", L"const wchar_t*", L"Hello" );
Where the output would be:
[int] 1, [const wchar_t*] Hello
To simplify the usage of this function, I intend to include a C++ template function like this:
template< class T1, class T2 >
void Foo( T1 p1, T2 p2 )
{
std::wstringstream t1W;
t1W << typeid( p1 ).name();
std::wstringstream p1W;
p1W << p1;
std::wstringstream t2W;
t2W << typeid( p2 ).name();
std::wstringstream p2W;
p2W << p2;
::DLL_Foo( 4, t1W.str().c_str(), p1W.str().c_str(), t2W.str().c_str(), p2W.str().c_str() );
};
Where the expected usage is like this:
int a = 1;
const wchar_t* b = L"Hello";
Foo( a, b );
With the same expected output as before.
Is there a template recursion method I can employ so that I don't have to implement a different template<> Foo() function for 0..n parameters?
template<> void Foo();
template< class T1 > void Foo( T1 p1 );
template< class T1, ..., class N > void Foo( T1 p1, ..., N n );
Please, no solutions involving variadic templates or other C++0x features. I realize they're wonderful, but I'm using VS2008. Also, just to be more difficult, I cannot make use of boost functionality like boost::MPL.
Thanks, PaulH
Edit: Yes, the DLL actual dll function does more than print the type and value information. The actual DLL function looks a bit more like this:
__declspec( dllexport ) void DLL_Foo( MYHANDLE handle, int count, ... )
{
CMyObject* obj = reinterpret_cast< CMyObject* >( handle );
va_list list;
for( va_start( list, count ); count; --count )
{
const wchar_t* item = va_arg( list, const wchar_t* );
if( count % 2 == 0 )
{
obj->AddTypeInfo( item );
}
else
{
obj->AddValueInfo( item );
}
}
va_end( list );
}