タプルが最初に検出したタイプUを線形検索し、Uが見つからない場合はコンパイル時エラーが発生するコードを次に示します。タプルに複数のUが含まれている場合は、最初のタイプのみが検出されます。それがあなたが望む方針であるかどうかわからない。コンパイル時のインデックスを最初のUのタプルに返します。おそらく、それをのインデックスとして使用できますstd::get
。
免責事項:この答えのために一緒に投げられます。軽くテストされただけです。空のタプルなどのエッジケースには、改善できる厄介なエラーメッセージがあります。等
#include <type_traits>
#include <tuple>
template <class Tuple, class T, std::size_t Index = 0>
struct find_first;
template <std::size_t Index, bool Valid>
struct find_first_final_test
: public std::integral_constant<std::size_t, Index>
{
};
template <std::size_t Index>
struct find_first_final_test<Index, false>
{
static_assert(Index == -1, "Type not found in find_first");
};
template <class Head, class T, std::size_t Index>
struct find_first<std::tuple<Head>, T, Index>
: public find_first_final_test<Index, std::is_same<Head, T>::value>
{
};
template <class Head, class ...Rest, class T, std::size_t Index>
struct find_first<std::tuple<Head, Rest...>, T, Index>
: public std::conditional<std::is_same<Head, T>::value,
std::integral_constant<std::size_t, Index>,
find_first<std::tuple<Rest...>, T, Index+1>>::type
{
};
#include <iostream>
int main()
{
typedef std::tuple<char, int, short> T;
std::cout << find_first<T, double>::value << '\n';
}