简体   繁体   中英

Mapping a std::tuple to another std::tuple

Let's assume I have a std::tuple<Types...> , I want to generate a std::tuple<std::priority_queue<Types...>>

#include <queue>
#include <tuple>

template<typename TypesTuple>
struct Bar {
    std::priority_queue<std::for_each_tuple_type(TypesTuple)...> queues;
};

using Foo = Bar<std::tuple<int, double>>;

How can that be achieved?

EDIT

Could it get easier with boost::tuple or vector and transform? Can transform be applied on the meta level?

You can use partial specialization:

template<typename TypesTuple>
struct Bar;

template<typename... Ts>
struct Bar<std::tuple<Ts...>> {
    std::tuple<std::priority_queue<Ts>...> queues;
};

So my_bar.queue will be of type std::tuple<std::priority_queue<int>, std::priority_queue<double>> .

Live demo

Another way:

template<class... Types>
std::tuple<std::priority_queue<Types...>> convert(std::tuple<Types...>);

using Foo = decltype(convert(std::declval<std::tuple<int>>());

However, that only works if the tuple contains 1 to 3 elements and they satisfy std::priority_queue template argument requirements, eg:

using Foo = decltype(convert( std::declval<std::tuple<int, std::vector<int>, std::less<int> > >() ));
template<class In, template<class...>class Map>
struct template_map;
template<class In, template<class...>class Map>
using template_map_t=typename template_map<In, Map>::type;

template<template<class...>class Z, class...Ts, template<class...>class Map>
struct template_map<Z<Ts...>, Map> {
  using type=Z<Map<Ts>...>;
};

This takes a type that is a template over types, and a type map, and returns the type after you map the enclosed types by the map.

template<typename TypesTuple>
struct Bar {
  template_map_t<TypesTuple, std::priority_queue> queues;
};

using Foo = Bar<std::tuple<int, double>>;

Writing template_map_t can be done in many ways.


While I wouldn't advise it, here is a hana-style:

template<class T>struct tag_t{ constexpr tag_t() {}; using type=T; };
template<class T>constexpr tag_t<T> tag{};

template<template<class...>class Z>
struct ztemplate_t{
  constexpr ztemplate_t() {};
  template<class...Ts>using apply=Z<Ts...>;
};
template<template<class...>class Z>
constexpr ztemplate_t<Z> ztemplate{};

These are constexpr values which store types and templates respectively.

template<class Z, class...Ts>
constexpr auto zapply( Z, tag_t<Ts>... )
-> tag_t< typename Z::template apply<Ts...> >
{ return {}; }

zapply now lets us use values to apply templates to types.

We can now write a map function:

template<template<class...>class Z, class...Ts, class zM>
constexpr auto zmap( tag_t<Z<Ts...>>, zM )
{ return zapply( ztemplate<Z>, zapply( zM{}, tag<Ts> )... ); }

and extract the type:

template<class Tag>using type_t=typename Tag::type;

template<typename TypesTuple>
struct Bar {
  using queues_t = type_t<decltype(
    zmap( tag<TypesTuple>, ztemplate<std::priority_queue> )
  )>;
  queues_t queues;
};

with test code:

using Foo = Bar<std::tuple<int, double>>;

tag_t< std::tuple< std::priority_queue<int>, std::priority_queue<double> > > test = tag< decltype( std::declval<Foo&>().queues ) >;

which shows the type map worked.

Live example .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM