Второй плохой по многим причинам.
Во-первых, звонить - это беспорядок. Шаблоны внутри шаблонов требуют использования ключевого слова template
.
Во-вторых, он требует, чтобы ваш список типов включал все операции, которые вы хотите выполнять над списками типов в его теле. Это похоже на определение каждой операции над string
как метода над строкой: если вы разрешаете свободные функции, можно создавать новые операции и даже реализовывать переопределения.
Наконец, подумайте о том, чтобы скрыть ::type
:
Начните с этих примитивов:
template<class T>struct tag{using type=T;};
template<class Tag>using type_t=typename Tag::type;
template<class...Ts>struct types : tag<types<Ts...>>{};
преобразование, или fmap
, выглядит следующим образом:
template<template<class...>class Z, class Types>
struct fmap;
template<template<class...>class Z, class...Ts>
struct fmap<Z, types<Ts...>>:types<Z<Ts...>>{};
template<template<class...>class Z, class Types>
using fmap_t = type_t<fmap<Z,Types>>;
и вы можете использовать type_t<fmap<Z,types<int,double>>>
или fmap_t<Z,types<int,double>>
для получения типов сопоставленного типа.
Еще один подход заключается в использовании constexpr
функций, которые содержат различные вещи:
template<class T>struct tag{using type=T;};
template<class...>struct types{using type=types;};
template<class Tag>using type_t=typename Tag::type;
template<template<class...>class Z>
struct z {template<class...Ts>using apply=Z<Ts...>; constexpr z(){};};
template<class...Ts>
struct one_type {};
template<class T0>
struct one_type<T0> { using type=T0; };
template<class...Ts>
using one_type_t=typename one_type<Ts...>::type;
template<template<class>class Z>
struct z_one_base {
template<class...Ts>
using helper = Z<one_type_t<Ts...>>;
using type = z<helper>;
};
template<template<class>class Z>
using z_one = type_t<z_one_base<Z>>;
теперь fmap
просто:
// take a template metafunction and a list of types
// and apply the metafunction to each type, returning the list
template<template<class...>class Z, class...Ts>
constexpr auto fmap( z<Z>, types<Ts...> )
-> types<Z<Ts>...> { return {}; }
и другие функции следуют:
// a template metafunction and a list of types
// and apply the template metafunction to all of the types
template<template<class...>class Z, class...Ts>
constexpr auto apply( z<Z>, types<Ts...> )
-> tag<Z<Ts...>> { return {}; }
// take any number of tags
// and make a type list from them
template<class...Tags>
constexpr auto make_list( Tags... )
-> types<type_t<Tags>...> { return {}; }
// concat of nothing is an empty list
constexpr types<> concat() { return {}; }
// concat of a list alone is a list alone:
template<class...T1s>
constexpr auto concat(types<T1s...>)
->types<T1s...>{ return {}; }
// concat of 2 or more lists is the concat of the first two,
// concatted with the rest
template<class...T1s, class...T2s, class...Types>
constexpr auto concat(types<T1s...>,types<T2s...>,Types...)
->decltype( concat(types<T1s...,T2s...>{},Types{}...) )
{ return {}; }
// take a tagged list or a tagged type, and return a list
template<class T>
constexpr auto fbox( tag<T> )->types<T> { return {}; }
template<class...Ts>
constexpr auto fbox( tag<types<Ts...>> )->types<Ts...> { return {}; }
// create z_ versions of functions above:
#define CAT2(A,B) A##B
#define CAT(A,B) CAT2(A,B)
// lift functions to metafunctions with z_ prefix:
#define Z_F(F) \
template<class...Ts> \
using CAT(meta_, F) = decltype( F( Ts{}... ) ); \
using CAT(CAT(z_, F),_t) = z<CAT(meta_, F)>; \
static constexpr CAT(CAT(z_, F),_t) CAT(z_, F){}
Z_F(concat);
//Z_F(apply);
//Z_F(fmap);
Z_F(fbox);
static constexpr z_one<tag> z_tag{};
// joins a list of lists or types into a list of types
template<class...Ts>
constexpr auto join1(types<Ts...>)
->type_t<decltype( apply( z_concat, fmap( z_fbox, types<tag<Ts>...>{} ) ) )>
{ return {}; }
template<class Types>
constexpr auto join(Types types)
->type_t<decltype( apply( z_concat, fmap( z_fbox, fmap( z_tag, types ) ) ) )>
{ return {}; }
template<class Z, class...Ts>
constexpr auto fbind(Z z, Ts...ts)
->decltype( join( fmap( z, ts... ) ) )
{ return {}; }
и работать с псевдотипами (tag
s) вместо типов напрямую на верхнем уровне. Если вам нужно вернуться к типам с type_t
, когда вы хотите.
Я думаю, что это подход, похожий на boost::hana
, но я только начал рассматривать boost::hana
. Преимущество здесь в том, что мы отделяем пакеты типов от операций, получаем доступ к полной перегрузке C++ (вместо сопоставления шаблонов с шаблонами, которое может быть более хрупким), и мы получаем возможность напрямую выводить содержимое пакетов типов без необходимости проделайте трюки с using
и пустой первичной специализацией.
Все, что потребляется, является обернутым типом tag<?>
, types<?>
или z<?>
, поэтому ничто не является «настоящим».
Тестовый код:
template<class T> using to_double = double;
template<class T> using to_doubles = types<double>;
int main() {
types< int, int, int > three_ints;
auto three_double = fmap( z_one<to_double>{}, three_ints );
three_double = types<double, double, double >{};
auto three_double2 = join( fmap( z_one<to_doubles>{}, three_ints ) );
three_double = three_double2;
auto three_double3 = fbind( z_one<to_doubles>{}, three_ints );
three_double3 = three_double2;
}
Живой пример.
person
Yakk - Adam Nevraumont
schedule
12.06.2015