fork download
  1. #include <iostream>
  2. #include <typeinfo>
  3.  
  4. // mapping :: TYPE -> TYPE
  5. // ---------------------------------------------------------
  6. // ?? --> int (default "value")
  7. template<typename X> struct mapping {
  8. using type = int;
  9. };
  10. // if instead you want it to be undefined for unknown types:
  11. //template<typename X> struct mapping;
  12. // bool --> double
  13. template<> struct mapping<bool> {
  14. using type = double;
  15. };
  16.  
  17.  
  18.  
  19. // map :: ([T] -> T) -> (T -> T) -> ([T] -> T)
  20. // "List" "Mapping" result "type" (also a "List")
  21. // --------------------------------------------------------
  22. template<template<typename...> class List,
  23. template<typename> class Mapping>
  24. struct map {
  25. template<typename... Elements>
  26. using type = List<typename Mapping<Elements>::type...>;
  27. };
  28.  
  29.  
  30. template<typename...> struct Example {};
  31.  
  32.  
  33.  
  34. template<typename... S>
  35. using MappedExample = map<Example, mapping>::type<S...>;
  36.  
  37. template<typename... S>
  38. MappedExample<S...> f() {
  39. return MappedExample<S...>{};
  40. }
  41.  
  42.  
  43. int main() {
  44. std::cout
  45. << typeid(Example<bool,int,char,double>).name()
  46. << std::endl
  47. << typeid(decltype(f<bool, int, char, double>())).name()
  48. << std::endl;
  49. }
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
7ExampleIJbicdEE
7ExampleIJdiiiEE