1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
| #include <type_traits> #include <tuple> #include <iostream>
template<int ...values> struct Values { using type = std::tuple<std::integral_constant<int,values>...>; };
template<bool,typename Then,typename Else> struct If;
template<typename Then,typename Else> struct If<true,Then,Else> { using type = Then; };
template<typename Then,typename Else> struct If<false,Then,Else> { using type = Else; };
template<typename T1> struct Less { template<typename T2> struct Apply { using type = typename If<(T1::value < T2::value),T1,T2>::type; }; };
template<typename T1> struct GE { template<typename T2> struct Apply { using type = typename If<(T1::value >= T2::value),T1,T2>::type; }; };
template<template <typename T1> class Comp, typename Tuples> struct FindLimit; template<template <typename T1> class Comp, typename Head> struct FindLimit<Comp,std::tuple<Head>> { using type = Head; }; template<template<typename T1> class Comp,typename Head,typename ...Tails> struct FindLimit<Comp,std::tuple<Head,Tails...>> { using type =typename Comp<Head>::template Apply< typename FindLimit<Comp,std::tuple<Tails...>>::type >::type; };
template<typename Limit,typename ...Tuples> struct PopLimit;
template<typename Limit> struct PopLimit<Limit,std::tuple<>> { using type = std::tuple<>; };
template<typename Limit,typename ...Others>
struct PopLimit<Limit,std::tuple<Limit,Others...>> { using type = std::tuple<Others...>; };
template<typename Limit,typename Head,typename ...Others> struct PopLimit<Limit,std::tuple<Head,Others...>> { using type = decltype(std::tuple_cat(std::tuple<Head>{}, typename PopLimit<Limit,std::tuple<Others...>>::type{})); };
template<template <typename T1> class Comp,typename T> struct bSort;
template<template <typename T1> class Comp> struct bSort<Comp,std::tuple<>> { using type = std::tuple<>; };
template<template <typename T1> class Comp,typename ...values> struct bSort<Comp,std::tuple<values...>> { using limit = typename FindLimit<Comp,std::tuple<values...>>::type; using others = typename PopLimit<limit,std::tuple<values...>>::type; using type = decltype(std::tuple_cat(std::tuple<limit>{},typename bSort<Comp,others>::type{}));
};
void test() { using lst_1_3 = Values<1,2,3>::type; using lst_1_2 = Values<1,2>::type; using lst_3_1 = Values<3,2,1>::type; using lst = Values<1,99,9,4,2,6,100>::type; using ans = Values<1,2,4,6,9,99,100>::type;
using i1 = std::integral_constant<int,1>::type; using i2 = std::integral_constant<int,2>::type; using i3 = std::integral_constant<int,3>::type;
static_assert(!std::is_same<FindLimit<GE,lst>::type,i1>::value, "最小值是 i1");
}
int main(int argc,char *argv[]) { test(); return 0; }
|