HTAPAWASO
HTAPAWASO

Reputation: 70

Expanding container to parameter pack

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.

Upvotes: 3

Views: 1021

Answers (1)

coyotte508
coyotte508

Reputation: 9705

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.

Upvotes: 5

Related Questions