我在this answer中遇到过这段代码,我无法解释它究竟做了什么,以及如何将它用作整数的编译时序。有人可以解释用递归模板生成整数序列吗?
template <size_t ...I>
struct index_sequence {};
template <size_t N, size_t ...I>
struct make_index_sequence : public make_index_sequence<N - 1, N - 1, I...> {};
template <size_t ...I>
struct make_index_sequence<0, I...> : public index_sequence<I...> {};
我发现以前类似的实现,即here和here,但从来不知道如何来理解语法。 我不明白make_index_sequence
的两个模板结构定义,特别是public
(例如: public make_index_sequence<N - 1, N - 1, I...>
)的部分。有人可以向我解释这一点吗?
这里有继承吗?递归如何工作?序列存储在哪里?
编辑:由这些模板生成的整数序列的示例用法是生成访问std::tuple<>
的索引。生成器将在编译时为元组中的每个元素生成一个索引。
的可变参数模板参数包的':public'部分是一个暗示,继承是怎么回事。为什么你会这么做我不确定。你的目标是什么? – tadman