Ответ 1
Вторая причина плохо по многим причинам.
Во-первых, называть это беспорядок. Шаблоны внутри шаблонов требуют использования ключевого слова 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...>>{};
transform или 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 {}; }
и работать с psuedo-типами (tag
s) вместо типов непосредственно на верхнем уровне. Если вам нужно вернуться к типам с помощью type_t
, если вы хотите.
Я думаю, что это подход boost::hana
, но я только начал смотреть на boost::hana
. Преимущество здесь в том, что мы отделяем пулы типов от операций, получаем доступ к полной перегрузке С++ (вместо сопоставления шаблонных шаблонов, которые могут быть более хрупкими), и мы получаем прямое вывод содержимого наборов типов без необходимости выполните теги 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;
}