如何展平嵌套模板参数?

时间:2019-01-03 16:01:23

标签: c++ templates variadic-templates

说我有

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

我想转换

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

进入

pack<int, int, int, int>

我该怎么办?

7 个答案:

答案 0 :(得分:2)

可能基于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

答案 1 :(得分:2)

我提出以下结构并使用

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;

通过这种方式,您可以将pack和其他模板范本展平为std::tuple,还可以展平更复杂的示例(例如Holt建议的pack<int, pack<int, int>, int>)。

如果您只想pack扁平化,那么避免所有模板都扁平化...,我的意思是...如果您想要

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

展平为

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

代替

pack<int, int, int, long>

您必须在上一个flatt_helper专业化中删除template-template参数,并将其简化如下

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

以下是完整的编译示例(具有完整的拼合)

#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, "!" );
 }

答案 2 :(得分:1)

我将以递归方式解包然后再打包:

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...>>;
};

类型repack<pack<int, pack<int, pack<int, pack<int>>>>>::type转换为:

  • 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

答案 3 :(得分:1)

晚会了吗?

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>>);

答案 4 :(得分:0)

pack_cat接受一系列的包装或非包装,并将包含包装的所有内容与不含包装的物品一起混合在一起。

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打包,然后递归解压缩所有子包并将其打包。

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>...>;};

要测试:

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>>{};

编译IFF的两种类型是相同的。

答案 5 :(得分:0)

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 > > >()... ) );

答案 6 :(得分:0)

现在...对于完全不同的东西...(嗯...不完全...几乎是Holt的解决方案,而是使用函数而不是结构)

您可以在几个声明的函数中将std::tuple_cat()decltype()组合在一起,以将pack放在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>())...) );

和另一个声明的函数,这些函数再次将展平的std::tuple转换为pack

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

现在,您可以按以下步骤组合拼合器

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

以下是完整的编译示例

#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, "!" );
 }