Variadic varadic template template options

Is there an easy way to have variable template template options. For example, consider the following function signature

template<template<typename,size_t...> class Pack, typename T, size_t ... Args> void foo(const Pack<T,Args...>& a); 

If we want to transfer two Pack , we have to do an overload

 template<template<typename,size_t...> class Pack, typename T, size_t ... Args0, size_t ... Args1> void foo(const Pack<T,Args0...>& a, const Pack<T,Args1...>& b); 

Now, if we want to pass a variable number of Pack objects with different variation parameters, for example. Args0...,Args1...,Args2...

So, I was wondering if there is a practical way to do something line by line (the following, of course, is a sketch view).

 template<template<typename,size_t...> ... class Pack, typename T,...> void foo(const Pack<T,...> ... packs); 
+7
c ++ c ++ 11 variadic-functions variadic-templates template-templates
source share
2 answers

I would just use a regular variational pattern:

 template<typename... Ts> void foo(const Ts&... packs) { } 

Then write the slash to extract the type and size_t s. You can easily add some helper alias patterns to do whatever you like.

 template <typename T> struct extract_args; template <template <typename, size_t...> class Pack, typename T, size_t... Args> struct extract_args<Pack<T,Args...>> { using type = T; using args = std::index_sequence<Args...>; }; 

Then in foo you can extract the arguments and use them as you like. For example, to get std::tuple containing all T from packages:

 using all_ts = std::tuple<typename extract_args<Ts>::type...>; 
+5
source share

I suggest you recursively manipulate the arguments of the Pack in this way

 #include <array> template <typename T, std::size_t ... Args> struct testS { }; void foo () { /* terminal iteration */ } template <template <typename, std::size_t...> class Pack, typename T, std::size_t ... Args, typename ... Packs> void foo (const Pack<T, Args...> pack0, Packs ... packs) { // do something with pack0 foo(packs...); } int main() { foo(testS<int>{}, std::array<long, 5U>{}, testS<char, 7U, 9U, 11U>{}); return 0; } 

--- Edit ---

A modified example to show the use of std::size_t with various types and variable number of template parameters.

+3
source share

All Articles