2017-01-10 5 views
3

タイプのインデックスを取得:私はtypelistにTypesの内側種類T2のインデックスを把握したいはこのように、私はシンプルなタイプリストのimplimentationを持っているタイプリスト

template<typename... Ts> 
struct TypeList 
{ 
    static constexpr std::size_t size{ sizeof... (Ts) }; 
}; 

struct T1 
    { 
    }; 

struct T2 
     { 
     }; 

struct T3 
     { 
     }; 

using Types = mpl::TypeList<T1, T2, T3>; 

。 これは私が現在使用しているものですが、検索しているタイプがタイプリストの冒頭にある場合にのみ機能します。それ以外の場合は、エラー "value:宣言されていない識別子"でコンパイルされます。

template<typename, typename> 
struct IndexOf {}; 

// IndexOf base case: found the type we're looking for. 
template <typename T, typename... Ts> 
struct IndexOf<T, TypeList<T, Ts...>> 
    : std::integral_constant<std::size_t, 0> 
{ 
}; 



// IndexOf recursive case: 1 + IndexOf the rest of the types. 
template <typename T, typename TOther, typename... Ts> 
struct IndexOf<T, TypeList<TOther, Ts...>> 
    : std::integral_constant<std::size_t, 
    1 + IndexOf<T, Ts...>::value> 
{ 
}; 
+0

「IndexOf」の定義が、[jbabの修正](http://stackoverflow.com/a/41579685/5386374)を適用した後であっても、私はあなたのコードを['std :: cout << IndexOf :: value'](http://ideone.com/34KEN8)。 –

+0

'(here's) '(http://ideone.com/thAkQG)' static_cast (-1) 'を「見つからない」として使用してその問題を回避し、' constexpr'関数を使ってタイプリスト。 –

答えて

4

IndeOf<T, Ts...>::value 

が定義されていないので、あなたは、エラーが発生します。

IndexOf<T, TypeList<Ts...>>::value 

である必要があります。

関連する問題