- 作者: 雪山肥鱼
- 时间:2022027 12:42
- 目的: integer_sequence
integer_sequence 简介
integer_sequence 作用是产生一个整型数列。
template <class T, T... Ints>
struct integer_sequece {
using value_type = T;
static constexpr size_t size() noexcept
{
return sizeof...(Ints);
}
};
- Ints 是一个非类型模板参数包, T: int, unsigned int, short等。一堆数字而已。
- std::make_integer_sequence 别名模板,可以理解为 就是 std::integer_sequence:
template <class T, T N>
using make_integer_sequence = integer_sequnce<T, 0,1,2,3,4,...,N-1序列>
int main(int argc, char ** argv)
{
std::make_integer_sequence<int, 5> tmpobj;
cout << "tmpobj类型:" << typeid(decltype(tmpobj)).name() << endl;
return 0;
}
得到 元素类型 + 个数 0 - 4
- std::make_index_sequence
//伪代码
template<size_t N>
using make_index_sequence = integer_sequence<std::size_t, 0,1,2...N-1>
std::make_index_sequence<5> tmobj2;
cout << "tmpobj2类型:" << typeid(decltype(tmobj2)).name() << endl;
正向排列数字生成一个类型 integer_sequence
简单实现 make_integer_sequence
//向integer_sequence 末尾插入元素:
//泛化版本:
template<typename INSEQ, unsigned int NewElem>
struct IntSeq_PushBack;//不适用泛化版本,所以泛化版本 只声明不定义
//特化版本才插入元素
template<typename T, unsigned int... Elems, unsigned int NewElem>//特化版本int...可以放在中间,泛化版本必须放到最后
struct IntSeq_PushBack<std::integer_sequence<T, Elems...>, NewElem> {
using type = integer_sequence<T, Elems..., NewElem>;
};
//泛化
template <typename T, unsigned int N>
struct Integer_Sequence {
//依次遍历出 4 3 2 1,往末尾插入元素操作
using type = typename InSeq_PushBack<typename Integer_Sequence<T, N-1>::type, N-1>::type;//
};
//特化
template<typename T>
struct Integer_Sequence<T, 1>//递归到1
{
using type = std::integer_sequence<T, 0>
};
template <typename T, unsigned int N>
using Integer_Sequence_T = typename Integer_Sequence<T, N>::type;//功能上和 make_integer_sequence类似
int main(int argc, char ** argv)
{
std::make_integer_sequence<int, 5> tmpobj;
cout << "tmpobj类型:" << typeid(decltype(tmpobj)).name() << endl;
std::make_index_sequence<5> tmobj2;
cout << "tmpobj2类型:" << typeid(decltype(tmobj2)).name() << endl;
Integer_Sequence_T<int, 4> tmpobj3;
cout << "tmpobj3类型:" << typeid(decltype(tmpobj3)).name() << endl;
return 0;
}
逆向排列数字生成一个类型
template<typename INSEQ, unsigned int NewElem>
struct IntSeq_PushFront;
template<typename T, unsigned int ... Elems, unsigned int NewElem>
struct IntSeq_PushFront<integer_sequence<T, Elems...>, NewElem> {
using type = integer_sequence<T, NewElem,Elems...>;
};
template <typename T, unsigned int N, unsigned Count =1>
struct Integer_Sequence_Revers{
using type = typename IntSeq_PushFront<typename Integer_Sequence_Revers<T, N - 1>::type, N - Count>::type;
};
template <typename T, unsigned int N>
struct Integer_Sequence_Revers<T, N, N>
{
using type = integer_sequence<T, N-1>;
};
template<typename T, unsigned int N>
using Integer_Sequence_Revers_T = typename Integer_Sequence_Revers<T, N>::type;
int main(int argc, char ** argv) {
Integer_Sequence_Revers_T<int, 4> tmpobj4;
cout << typeid(decltype(tmpobj4)).name() << endl;
return 0;
}
- 注意上述代码中 count 并没有自增
- 直到N == Count == 1 时,才会走特化版本。
Repeat Integer
有空再回过头来看。