專業化是終止條件。請注意,它需要First
等於Search
:
type_index<Index, Search, Search, Types ...>
^^^^^^ ^^^^^^
例如,如果您有
type_index<0, C, A, B, C, D>,
這不符合專業化,所以通用模板將被使用,重定向啓動(通過其type
部件)
type_index<0, C, A, B, C, D>::type = type_index<1, C, B, C, D>::type
但是這不可求值或者直到達到鏈
type_index<0, C, A, B, C, D>::type = ... = type_index<2, C, C, D>::type
此時部分特可以被使用,它說,
type_index<2, C, C, D>::type = type_index<2, C, C, D>
type_index<2, C, C, D>::index = 2
等
type_index<0, C, A, B, C, D>::type::index = 2
^^^^
0 1 2 3
如預期。
請注意,您不需要攜帶Index
周圍,確實可以放下整個::type
事情:
template<typename, typename...>
struct type_index;
template<typename Search, typename Head, typename... Tail>
struct type_index<Search, Head, Tail...> {
// Search ≠ Head: try with others, adding 1 to the result
static constexpr size_t index = 1 + type_index<Search, Tail...>::index;
};
template<typename Search, typename... Others>
struct type_index<Search, Search, Others...> {
// Search = Head: if we're called directly, the index is 0,
// otherwise the 1 + 1 + ... will do the trick
static constexpr size_t index = 0;
};
template<typename Search>
struct type_index<Search> {
// Not found: let the compiler conveniently say "there's no index".
};
這個工作過程:
type_index<C, A, B, C, D>::index
= 1 + type_index<C, B, C, D>::index
= 1 + 1 + type_index<C, C, D>::index
= 1 + 1 + 0
= 2
,如果類型不在該列表中,會說像(GCC 6.2.1):
In instantiation of ‘constexpr const size_t type_index<X, C>::index’:
recursively required from ‘constexpr const size_t type_index<X, B, C>::index’
required from ‘constexpr const size_t type_index<X, A, B, C>::index’
required from [somewhere]
error: ‘index’ is not a member of ‘type_index<X>’
我覺得很明顯。
我認爲相應的作者應該首先使用'TODO'... ;-) –
如果「搜索」類型不在其中,則會生成*可愛*錯誤消息搜索... – Walter