karatoga
karatoga

Reputation: 573

how to flatten nested templates parameters?

Say I have

template<class ... T> pack { };

I want to convert

pack<int, pack<int, pack<int, pack<int>>>>

into

pack<int, int, int, int>

How can I do so?

Upvotes: 1

Views: 388

Answers (7)

Jans
Jans

Reputation: 11250

Late to the party?

template <class... Ts> struct flatten;
template <class... Ts> struct flatten<pack<Ts...>, pack<>>
{
    using type = pack<Ts...>;
};
template <class... Ts> struct flatten<pack<Ts...>>
    : flatten<pack<>, pack<Ts...>>
{ };
template <class... Ts, class T, class... Rs>
struct flatten<pack<Ts...>, pack<T, Rs...>> : flatten<pack<Ts...>, T, pack<Rs...>>
{ };
template <class... Ts, class T, class... Es>
struct flatten<pack<Ts...>, T, pack<Es...>> : flatten<pack<Ts..., T>, pack<Es...>>
{ };
template <class... Ts, class... Rs, class... Es>
struct flatten<pack<Ts...>, pack<Rs...>, pack<Es...>> : flatten<pack<Ts...>, pack<Rs..., Es...>>
{ };

template <class T> using flatten_t = typename flatten<T>::type;


using T1 = pack<int, pack<int, int>, pack<int, int>, int>;
using T2 = pack<int, pack<int, pack<int, pack<int, int>>>, int>;
using R1 = pack<int,int,int,int,int,int>;

static_assert(std::is_same_v<R1, flatten_t<T1>>);
static_assert(std::is_same_v<R1, flatten_t<T2>>);

Upvotes: 1

max66
max66

Reputation: 66210

And now... for something completely different... (well... not completely... almost the Holt's solutions but using functions instead of structs)

You can combine std::tuple_cat() with decltype() in a couple of declared function to flat a pack in a std::tuple

template <typename T>
constexpr std::tuple<T> foo (T);

template <typename ... Ts>
constexpr auto foo (pack<Ts...>)
   -> decltype( std::tuple_cat(foo(std::declval<Ts>())...) );

and another declared functions that convert the flattened std::tuple again in a pack

template <typename ... Ts>
constexpr pack<Ts...> bar (std::tuple<Ts...>);

Now you can combine in a flattener as follows

template <typename T>
using fl = decltype(bar(foo(std::declval<T>())));

The following is a full compiling example

#include <tuple>
#include <type_traits>

template <typename...>
struct pack
 { };

template <typename T>
constexpr std::tuple<T> foo (T);

template <typename ... Ts>
constexpr auto foo (pack<Ts...>)
   -> decltype( std::tuple_cat(foo(std::declval<Ts>())...) );

template <typename ... Ts>
constexpr pack<Ts...> bar (std::tuple<Ts...>);

template <typename T>
using fl = decltype(bar(foo(std::declval<T>())));

int main()
 {
   using U0 = pack<int, pack<int, pack<int, pack<int>>>>;
   using U1 = pack<int, int, int, int>;
   using U2 = fl<U0>;
   using U3 = pack<int, pack<int, long>, pack<pack<int, char>, long>>;
   using U4 = pack<int, int, long, int, char, long>;
   using U5 = fl<U3>;

   static_assert( std::is_same<U1, U2>::value, "!" );
   static_assert( std::is_same<U4, U5>::value, "!" );
 }

Upvotes: 0

Red.Wave
Red.Wave

Reputation: 4225

template< typename >
struct is_tuple: false_type{};

template<typename ... input_t>
struct is_tuple< std::tuple<input_t ... > > :
    true_type{};

template<typename ... input_t> using flat_tuple= decltype(std::tuple_cat( std::declval< std::conditional_t< is_tuple< input_t >::value, input_t, std::tuple< input_t > > >()... ) );

Upvotes: 0

Yakk - Adam Nevraumont
Yakk - Adam Nevraumont

Reputation: 275500

pack_cat takes a sequence of packs or non-packs, and concatinates anything that is a pack together, plus items that are not packs.

template<class T0, class...Ts>
struct catter;
template<class...Ts>
using pack_cat = typename catter<Ts...>::type;
template<class T0>
struct catter<T0>{ using type=T0; };
template<class...Ts, class...Us, class...Vs>
struct catter<pack<Ts...>, pack<Us...>, Vs...>{ using type=pack_cat<pack<Ts...,Us...>,Vs...>; };
template<class...Ts, class U, class...Vs>
struct catter<pack<Ts...>, U, Vs...>{ using type=pack_cat<pack<Ts...,U>,Vs...>; };

unpacker takes a pack, and recursively unpacks all sub-packs and concatinates them.

template<class X>
struct unpacker{using type=X;};
template<class X>
using unpack=typename unpacker<X>::type;
template<class...Ts>
struct unpacker<pack<Ts...>>{using type=pack_cat<pack<>,unpack<Ts>...>;};

To test it:

pack<int,int,int,int>{}=unpack<pack<int,pack<int,pack<int,pack<int>>>>>{};
pack<int,int,int>{} = unpack<pack<int,int,int>>{};
pack<int,int,int>{} = unpack<pack<pack<int,int>,int>>{};

which compile IFF the two types are the same.

Upvotes: 0

max66
max66

Reputation: 66210

I propose the following struct and using

template <typename T0, typename...>
struct flatt_helper
 { using type = T0; };

template <typename ... Ts1, typename T0, typename ... Ts2>
struct flatt_helper<pack<Ts1...>, T0, Ts2...>
   : flatt_helper<pack<Ts1..., T0>, Ts2...>
 { };

template <typename ... Ts1, template <typename ...> class C, 
          typename ... Ts2, typename ... Ts3>
struct flatt_helper<pack<Ts1...>, C<Ts2...>, Ts3...>
   : flatt_helper<pack<Ts1...>, Ts2..., Ts3...>
 { };

template <typename T>
using flatt = typename flatt_helper<pack<>, T>::type;

This way you can flatten pack and other template-template, as std::tuple, and also more complex examples (the pack<int, pack<int, int>, int> suggested by Holt, by example).

If you want flat only pack, avoiding that all template-template are flattened..., I mean... if you want that

pack<int, pack<int, std::tuple<int, long>>>

is flattened as

pack<int, int, std::tuple<int, long>>

instead of

pack<int, int, int, long>

you have to remove the template-template parameter in the last flatt_helper specialization and simplify it as follows

template <typename ... Ts1, typename ... Ts2, typename ... Ts3>
struct flatt_helper<pack<Ts1...>, pack<Ts2...>, Ts3...>
   : flatt_helper<pack<Ts1...>, Ts2..., Ts3...>
 { };

The following is a full compiling example (with full flatting)

#include <tuple>
#include <type_traits>

template <typename...>
struct pack
 { };

template <typename T0, typename...>
struct flatt_helper
 { using type = T0; };

template <typename ... Ts1, typename T0, typename ... Ts2>
struct flatt_helper<pack<Ts1...>, T0, Ts2...>
   : flatt_helper<pack<Ts1..., T0>, Ts2...>
 { };

template <typename ... Ts1, template <typename ...> class C, 
          typename ... Ts2, typename ... Ts3>
struct flatt_helper<pack<Ts1...>, C<Ts2...>, Ts3...>
   : flatt_helper<pack<Ts1...>, Ts2..., Ts3...>
 { };

template <typename T>
using flatt = typename flatt_helper<pack<>, T>::type;

int main()
 {
   using T0 = pack<int, pack<int, pack<int, pack<int>>>>;
   using T1 = pack<int, int, int, int>;
   using T2 = flatt<T0>;
   using T3 = pack<int, pack<int, long>, std::tuple<pack<int, char>, long>>;
   using T4 = pack<int, int, long, int, char, long>;
   using T5 = flatt<T3>;

   static_assert( std::is_same<T1, T2>::value, "!" );
   static_assert( std::is_same<T4, T5>::value, "!" );
 }

Upvotes: 2

Holt
Holt

Reputation: 37616

A possible quick implementation based on std::tuple_cat:

template <class T>
struct tuple_flatten {
    using type = std::tuple<T>;
};

template <class... Args>
struct tuple_flatten<pack<Args...>> {
    using type = decltype(std::tuple_cat(
        typename tuple_flatten<Args>::type{}...));
};

template <class T>
struct tuple_to_pack;

template <class... Args>
struct tuple_to_pack<std::tuple<Args...>> {
    using type = pack<Args...>;
};

template <class T>
struct flatten {
    using type = typename tuple_to_pack<
        typename tuple_flatten<T>::type>::type;
};

template <class T>
using flatten_t = typename flatten<T>::type;

Godbolt demo

Upvotes: 2

YSC
YSC

Reputation: 40080

i'd recursively unpack and pack things back:

template<class Head, class... Packed>
struct repack
{
    using type = Head;
};

template<class Head, class... Packed>
struct repack<pack<Head, pack<Packed...>>>
{
    using type = pack<Head, repack<Packed...>>;
};

The type repack<pack<int, pack<int, pack<int, pack<int>>>>>::type get transformed into:

  • pack<int, repack<pack<int, pack<int, pack<int>>>>>
  • pack<int, int, repack<pack<int, pack<int>>>>
  • pack<int, int, int, repack<pack<int>>>
  • pack<int, int, int, int>

Live demo

Upvotes: 1

Related Questions