Get index of a tuple element's type?

后端 未结 3 652
忘掉有多难
忘掉有多难 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:08

    template 
    struct Index;
    
    template 
    struct Index> {
        static const std::size_t value = 0;
    };
    
    template 
    struct Index> {
        static const std::size_t value = 1 + Index>::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).

提交回复
热议问题