How to build new types by expand a parameter pack together with another list of types?

61 views Asked by At

I have a parameter packed fuction as follows,

template<typename... Args>
void foo(const Args&... args)
{   
   bar( SomeClass<Args, ??> { args }... );
}

Without the ?? part above, this is a normal parameter pack expansion.

However, I would like ?? to be a type from typedef std::tuple<(a list of types computed somewhere else)> typeLists;, with the same sequence as the parameter pack.

I.e., for the first type in parameter pack, ?? should be the first type in typeLists, etc.

It's ensured that the number of types in typeLists is the same as the size of parameter pack.

Is there a way to implement this? I read some similar questions but none is exactly the same. I think it probably requires to use index_sequence but I'm not sure how.

Thanks a lot.

1

There are 1 answers

0
康桓瑋 On BEST ANSWER

I think it probably requires to use index_sequence but I'm not sure how.

Yes, it's very easy to do this using index_sequence:

template<typename... Args, std::size_t... Is>
void foo_impl(std::index_sequence<Is...>, const Args&... args)
{   
   using typeLists = std::tuple<Args...>;
   bar( SomeClass<Args, std::tuple_element_t<Is, typeLists>>{ args }... );
}

template<typename... Args>
void foo(const Args&... args)
{   
   foo_impl( std::index_sequence_for<Args...>{}, args... );
}