5
template<typename T, typename... Ts>
struct Index
{
enum {value = ???}
};
並假設T是Ts和TS具有不同類型之一,像
Index<int, int, double>::value is 0
Index<double, int, double>::value is 1
template<typename T, typename... Ts>
struct Index
{
enum {value = ???}
};
並假設T是Ts和TS具有不同類型之一,像
Index<int, int, double>::value is 0
Index<double, int, double>::value is 1
#include <type_traits>
#include <cstddef>
template <typename T, typename... Ts>
struct Index;
template <typename T, typename... Ts>
struct Index<T, T, Ts...> : std::integral_constant<std::size_t, 0> {};
template <typename T, typename U, typename... Ts>
struct Index<T, U, Ts...> : std::integral_constant<std::size_t, 1 + Index<T, Ts...>::value> {};
你可能想添加C++ 14時尚可變模板:
template <typename T, typename... Ts>
constexpr std::size_t Index_v = Index<T, Ts...>::value;
這幾乎是你去年的問題的重複:[獲取元組元素類型的索引?](http://stackoverflow.com/q/18063451) – dyp 2014-10-02 20:48:54
@dyp(問一個關於variardics的問題,+ +年)''... – Yakk 2014-10-03 13:47:19