Reputation: 1167
I wrote the following code to find the first index in a tuple containing a given type.
#include <cstdio>
#include <tuple>
#include <type_traits>
#include <utility>
namespace detail {
template <typename T, typename Tuple, std::size_t H, std::size_t... I>
constexpr std::size_t tuple_type_index_impl(Tuple tup, std::index_sequence<H, I...> seq, std::size_t ret = 0) {
if (std::is_same<T, typename std::tuple_element<H, Tuple>::type>::value)
ret = H;
else ret = tuple_type_index_impl<T>(tup, std::index_sequence<I...>{});
return ret;
}
template <typename T, typename Tuple, std::size_t H>
constexpr std::size_t tuple_type_index_impl(Tuple tup, std::index_sequence<H> seq) {
static_assert(std::is_same<T, typename std::tuple_element<H, Tuple>::type>::value, "type not in tuple!");
return H;
}
}
template <typename T, typename Tuple>
constexpr std::size_t tuple_type_index(Tuple tup) {
return detail::tuple_type_index_impl<T>(tup, std::make_index_sequence<std::tuple_size<Tuple>::value>{});
}
class a {};
class b {};
class c {};
class d {};
std::tuple<a, b, c> abc;
int main() {
printf("b is index : %zu\n", tuple_type_index<d>(abc));
system("pause");
return 0;
}
The problem that I have is if you try to find the index for any type but c
the static_assert
will trigger. I'm not even sure why it's going all the way to the ending function when you try to find the index of types a
or b
.
If you remove the static_assert
the return values are correct, but it will return a value for types that are not in the tuple.
Inspired by the marked answer, I have reworked my implementation.
This version should work on clang++ and g++ instead of just on MSVC.
Upvotes: 0
Views: 158
Reputation: 66200
I suppose that the problem is that you don't write a right terminal (index sequence empty) tuple_type_index_impl()
function.
En passant, 0
value (in case of "type not found") isn't a good idea (IMHO) because you could confuse with "first type".
I've modified your example in the following
#include <tuple>
#include <utility>
#include <iostream>
#include <type_traits>
namespace detail
{
template <typename T, typename Tuple>
constexpr std::size_t tuple_type_index_impl
(const Tuple &,
const std::index_sequence<> &,
std::size_t ret = std::tuple_size<Tuple>::value)
{ return ret; }
template <typename T, typename Tuple, std::size_t H, std::size_t ... I>
constexpr std::size_t tuple_type_index_impl
(const Tuple & tup,
const std::index_sequence<H, I...> & seq,
std::size_t ret = std::tuple_size<Tuple>::value)
{
return std::is_same<T, typename std::tuple_element<H, Tuple>::type>::value
? H
: tuple_type_index_impl<T>(tup, std::index_sequence<I...>{}, ret);
}
}
template <typename T, typename Tuple>
constexpr std::size_t tuple_type_index (const Tuple & tup)
{
return detail::tuple_type_index_impl<T>
(tup, std::make_index_sequence<std::tuple_size<Tuple>::value>{});
}
class a {};
class b {};
class c {};
class d {};
int main()
{
std::tuple<a, b, c> abc;
std::cout << "a is index: " << tuple_type_index<a>(abc) << std::endl;
std::cout << "b is index: " << tuple_type_index<b>(abc) << std::endl;
std::cout << "c is index: " << tuple_type_index<c>(abc) << std::endl;
std::cout << "d is index: " << tuple_type_index<d>(abc) << std::endl;
return 0;
}
Give me some time and I try to prepare a simpler example.
--- EDIT: added example ---
You're working with types, so there isn't need of create and pass to function tuple (and index sequences) objects.
I've prepared an example based on a (SFINAE based) tupleTypeIndexHelper
struct; is working with types only so you use as
tuple_type_index<a, std::tuple<a, b, c>>()
or (when abc
is an istantiation of std::tuple<a, b, c>
) as
tuple_type_index<b, decltype(abc)>()
The following example works (at least: with g++ 4.9.2 and with clang++ 3.5) with C++11 too
#include <tuple>
#include <utility>
#include <iostream>
#include <type_traits>
namespace detail
{
template <typename T, typename Tuple, std::size_t Ind>
struct tupleTypeIndexHelper
{
static constexpr std::size_t dimT { std::tuple_size<Tuple>::value };
template <std::size_t I = Ind>
static typename std::enable_if<(I >= dimT), std::size_t>::type func ()
{ return dimT; }
template <std::size_t I = Ind>
static typename std::enable_if<(I < dimT), std::size_t>::type func ()
{
using typeI = typename std::tuple_element<I, Tuple>::type;
return std::is_same<T, typeI>::value
? I
: tupleTypeIndexHelper<T, Tuple, I+1U>::func();
}
};
}
template <typename T, typename Tuple>
constexpr std::size_t tuple_type_index ()
{ return detail::tupleTypeIndexHelper<T, Tuple, 0U>::func(); }
class a {};
class b {};
class c {};
class d {};
int main()
{
using t3 = std::tuple<a, b, c>;
std::cout << "a is index: " << tuple_type_index<a, t3>() << std::endl;
std::cout << "b is index: " << tuple_type_index<b, t3>() << std::endl;
std::cout << "c is index: " << tuple_type_index<c, t3>() << std::endl;
std::cout << "d is index: " << tuple_type_index<d, t3>() << std::endl;
std::cout << "int is index: " << tuple_type_index<int, t3>() << std::endl;
std::tuple<a, b, c> abc;
std::cout << "a is index: " << tuple_type_index<a, decltype(abc)>() << std::endl;
std::cout << "b is index: " << tuple_type_index<b, decltype(abc)>() << std::endl;
std::cout << "c is index: " << tuple_type_index<c, decltype(abc)>() << std::endl;
std::cout << "d is index: " << tuple_type_index<d, decltype(abc)>() << std::endl;
return 0;
}
Upvotes: 1