Get index of a tuple element's type?

后端 未结 3 609
忘掉有多难
忘掉有多难 2020-11-30 11:14

If I have a tuple with different element types like

std::tuple

And how to get the index of a element type?



        
相关标签:
3条回答
  • 2020-11-30 12:05
    template< size_t I, typename T, typename Tuple_t>
    constexpr size_t index_in_tuple_fn(){
        static_assert(I < std::tuple_size<Tuple_t>::value,"The element is not in the tuple");
    
        typedef typename std::tuple_element<I,Tuple_t>::type el;
        if constexpr(std::is_same<T,el>::value ){
            return I;
        }else{
            return index_in_tuple_fn<I+1,T,Tuple_t>();
        }
    }
    
    template<typename T, typename Tuple_t>
    struct index_in_tuple{
        static constexpr size_t value = index_in_tuple_fn<0,T,Tuple_t>();
    };
    

    The example above avoids generating tons of sub tuples, which makes compilation fail (out of memory) when you call index_in_tuple for large tuples

    0 讨论(0)
  • 2020-11-30 12:08
    template <class T, class Tuple>
    struct Index;
    
    template <class T, class... Types>
    struct Index<T, std::tuple<T, Types...>> {
        static const std::size_t value = 0;
    };
    
    template <class T, class U, class... Types>
    struct Index<T, std::tuple<U, Types...>> {
        static const std::size_t value = 1 + Index<T, std::tuple<Types...>>::value;
    };
    

    See it live at Coliru.

    This implementation returns the index of the first occurrence of a given type. Asking for the index of a type that is not in the tuple results in a compile error (and a fairly ugly one at that).

    0 讨论(0)
  • Yet another one using fold expression. It also sets the value to -1 when not found.

    template <class X, class Tuple>
    class Idx;
    
    template <class X, class... T>
    class Idx<X, std::tuple<T...>> {
        template <std::size_t... idx>
        static constexpr ssize_t find_idx(std::index_sequence<idx...>) {
            return -1 + ((std::is_same<X, T>::value ? idx + 1 : 0) + ...);
        }
    public:
        static constexpr ssize_t value = find_idx(std::index_sequence_for<T...>{});
    };
    

    live: https://onlinegdb.com/SJE8kOYdv

    0 讨论(0)
提交回复
热议问题