https://blog.csdn.net/huanghongxun/article/details/85065406
参考于上面的博客,但具体实现不一样。
#pragma once
#include<iostream>
namespace mystd {
template <typename T, T v>
struct integral_constant {
static constexpr T value = v;
typedef T value_type;
//constexpr operator T() const noexcept { return value; }
//constexpr T operator()() const noexcept { return value; }
};
template <int v>
using integer_constant = integral_constant<int, v>;
//实现列表结构
template <typename T, T... values>
struct integral_sequence { typedef T value_type;};
template <int ... values>
using integer_sequence = integral_sequence<int, values...>;
//求长
template<typename T,typename integral_sequence>
struct mylength {};
template<typename T,T ...data>
struct mylength<integral_sequence<T, data...>,T> :integral_constant<size_t, sizeof...(data)> {};
template<typename T>
using length=mylength<T, typename T::value_type>;
template<typename T,T data,typename Q>
struct push_front_t {};
template<typename T,T data,T ...datas>
struct push_front_t<T, data, integral_sequence<T, datas...>> { using type=integral_sequence<T, data, datas...>; };
template<int v,typename T>
using push_front=typename push_front_t<int, v, T>::type;
//push_back
template<typename T, T data, typename Q>
struct push_back_t {};
template<typename T, T data, T ...datas>
struct push_back_t<T, data, integral_sequence<T, datas...>> { using type=integral_sequence<T, datas...,data>; };
template<int v, typename T>
using push_back=typename push_back_t<int, v, T>::type;
//print
template <typename integer_sequence>
struct print { };
template<int start,int ...values>
struct print<integer_sequence<start, values...>> {
static void print_integer() {
std::cout << start << ' ';
print<integer_sequence<values...>>::print_integer();
}
};
template<>
struct print<integer_sequence<>> {
static void print_integer() { ; }
};
//empty
template<typename T>
struct empty;
template<>
struct empty<integer_sequence<>> :std::true_type {};
template<int ...value>
struct empty<integer_sequence<value...>> :std::false_type {};
//条件
template<bool con,typename T,typename F>
struct conditional {};
template<typename T,typename F>
struct conditional<true, T, F> { using type=T; };
template<typename T, typename F>
struct conditional<false, T, F> { using type=F; };
//map
template<template<int>typename Mapper, typename integer_sequence>
struct map_t;
template<template<int>typename Mapper>
struct map_t<Mapper, integer_sequence<>> {
using type=integer_sequence<>;
};
template<template<int>typename Mapper,int head,int ...tails>
struct map_t<Mapper, integer_sequence<head,tails...>> {
using type=push_front<Mapper<head>::value,typename map_t<Mapper,integer_sequence<tails...>>::type>;
};
template<template<int>typename Mapper,typename T>
using map = typename map_t<Mapper, T>::type;
template<int i>
struct increment {static constexpr int value=i+1;};
//
template<template<int,int>typename Mapper,int compare, typename integer_sequence>
struct filter;
template<template<int,int>typename Mapper,int compare>
struct filter<Mapper,compare, integer_sequence<>> { using type=integer_sequence<>; };
template<template<int,int>typename Mapper,int compare, int head, int ...tails>
struct filter<Mapper, compare,integer_sequence<head, tails...>> {
using type= typename conditional < Mapper<head,compare>::value,
push_front<head, typename filter<Mapper, compare,integer_sequence<tails...>>::type>,
typename filter<Mapper,compare,integer_sequence<tails...>>::type>::type;
};
template<int i,int j>
struct big {
static constexpr bool value = (i > j);
};
template<int i, int j>
struct low {
static constexpr bool value = (i <= j);
};
//CONCAT
template<typename T,typename Q>
struct concat;
template<typename T>
struct concat<T, integer_sequence<>> {
using type=T;
};
template<typename T,int head,int ...tail>
struct concat<T,integer_sequence<head, tail...>> {
using type=typename concat<push_back<head, T>, integer_sequence<tail...>>::type;
};
template<typename integer_sequence>
struct quick_sort {};
template<>
struct quick_sort<integer_sequence<>> {
using type=integer_sequence<>
;
};
template<int head,int ...tails>
struct quick_sort<integer_sequence<head,tails...> >{
using type=typename concat<typename quick_sort<typename filter<big,head,integer_sequence<tails...>>::type>::type,
typename concat<integer_sequence<head>,
typename quick_sort<typename filter<low,head,integer_sequence<tails...>>::type>::type>::type>::type;
};
}
#include<iostream>
#include"test.h"
#include<vector>
using namespace mystd;
void main() {
//print<filter<big,4,integer_sequence<1,2,3,4,5,6,7,8>>::type>::print_integer();
print<quick_sort<integer_sequence<3,434,45,3,5,465645,3,245,534,9>>::type>::print_integer();
}