如何从类型列表中删除元素

how can I delete an element from a type-list?

本文关键字:删除 元素 列表 类型      更新时间:2023-10-16

我已经使用 c++ 中的模板实现了类似方案的整数列表:

class Empty {};
template <int H, class T = Empty>
struct list {
  static const int head = H;
  typedef T tail;
};

我正在尝试实现排序功能,所以我决定使用函数:删除和find_min。算法是:在列表中找到最小值,创建一个以这个最小值作为头部的列表,并将其从尾部(列表的其余部分)中删除。重要的是,我希望它以递归方式实现,没有任何循环。我的min_function :

template <class T,int N>
struct min_elem{};

template<int H, class T,int N>
struct min_elem<list<H,T>,N >{

static const int value = (N>H ? min_elem<T,H>::value : min_elem<T,N>::value  );
};

template<int N>
struct min_elem<Empty,N>{
    static const int value=N;
};
template <class T>
struct min{};
template <int I, class T>
struct min<list<I,T> > {
    static const int value = min_elem<list<I,T>, I>::value;
};

但我不知道如何实现删除功能。我希望它类似于最小函数。谁能帮忙?功能的布局应该是:

template<class T, int K>
struct re{};
template<int I, class T, int K>
struct re <list<I,T>,K>{
     typedef list<  ?? don't know what should I exactly do here> value;
};
template<int I>
struct re <Empty,I>{
     ??
};

试试这种方式。

template<class T, int K> struct re;
template<int H, class T>
struct re <list<H, T>, H> { // the head and the subject are same value.
     typedef T type;
};
template<int H, class T, int K>
struct re <list<H, T>, K>{
     typedef list<H, typename re<T, K>::type> type;
};
template<int K>
struct re <Empty, K>{
     typedef Empty type;
};