3

Let's say I have a variadic function I can't change like so:

void Func(int count, ...)
{ /*etc.*/ }

And I'm writing a variadic template like so to wrap that function and make it safer to use:

template<typename... T>
void ToString(T... args)
{
    std::vector<CString> vstrArgs = { args... };
    Func(sizeof...(args), args...);
}

So my template verifies that each argument can be converted to a CString. I want to do more than that however, I want to convert each argument to a CString and then pass those CStrings to Func(), instead of just passing on the original arguments.

What's the fastest and/or neatest way to do this? Thanks.

HTAPAWASO
  • 70
  • 3
  • Possibly store the args parameter pack into a std::tuple and iterate over the tuple like here, checking that it can be converted and doing what you want : http://stackoverflow.com/questions/1198260/iterate-over-tuple – Chris Zhang May 11 '15 at 04:55
  • BTW, You should not pass arguments of non-POD type to VARARG'd functions. Even if [how-can-cstring-be-passed-to-format-string-s](http://stackoverflow.com/questions/6608942/how-can-cstring-be-passed-to-format-string-s). – Jarod42 May 11 '15 at 07:41

1 Answers1

5
template<typename... T>
void ToString(T... args)
{
    Func(sizeof...(args), ConvertToCString(args)...);
}

Where ConvertToCString is the function that converts any element to a CString. If the constructor is enough, then CString(args)... will work. static_cast<CString>(args)... would also work.

You can look at "Pack expansion" from parameter packs.

coyotte508
  • 9,175
  • 6
  • 44
  • 63