/tuple_element&action=edit&section=1
"Edit section: Member types") ]  Member types

Member type  |  Definition   
---|---  
` type ` |  ` T1 ` if ` I == 0 `   
` T2 ` if ` I == 1 `  
  
###  [ [ edit
](/mwiki/index.php?title=cpp/utility/pair/tuple_element&action=edit&section=2
"Edit section: Possible implementation") ]  Possible implementation

    
    
    template<[std::size_t](http://en.cppreference.com/w/cpp/types/size_t) I, typename T>
    struct tuple_element;
     
    template<[std::size_t](http://en.cppreference.com/w/cpp/types/size_t) I, typename T1, typename T2>
    struct tuple_element<I, [std::pair](http://en.cppreference.com/w/cpp/utility/pair)<T1, T2>>
    {
        static_assert(I < 2, "std::pair has only 2 elements!");
    };
     
    template<typename T1, typename T2>
    struct tuple_element<0, [std::pair](http://en.cppreference.com/w/cpp/utility/pair)<T1, T2>>
    {
        using type = T1;
    };
     
    template<typename T1, typename T2>
    struct tuple_element<1, [std::pair](http://en.cppreference.com/w/cpp/utility/pair)<T1, T2>>
    {
        using type = T2;
    };  
  
---  
  
###  [ [ edit
](/mwiki/index.php?title=cpp/utility/pair/tuple_element&action=edit&section=3
"Edit section: Example") ]  Example

Run this code

    
    
    #include <iostream>
    #include <string>
    #include <tuple>
     
    namespace detail
    {
        template<[std::size_t](http://en.cppreference.com/w/cpp/types/size_t)>
        struct index_tag { constexpr explicit index_tag() = default; };
     
        template<class T, class U>
        constexpr T get_val_dispatch([std::pair](http://en.cppreference.com/w/cpp/utility/pair)<T, U> const& pair, index_tag<0>)
        {
            return pair.first;
        }
     
        template<class T, class U>
        constexpr U get_val_dispatch([std::pair](http://en.cppreference.com/w/cpp/utility/pair)<T, U> const& pair, index_tag<1>)
        {
            return pair.second;
        }
    } // namespace detail
     
    template<[std::size_t](http://en.cppreference.com/w/cpp/types/size_t) N, class T, class U>
    auto constexpr get_val([std::pair](http://en.cppreference.com/w/cpp/utility/pair)<T, U> const& pair)
        -> typename [std::tuple_element](http://en.cppreference.com/w/cpp/utility/tuple_element)<N, [std::pair](http://en.cppreference.com/w/cpp/utility/pair)<T, U>>::type
    {
        return detail::get_val_dispatch(pair, detail::index_tag<N>{});
    }
     
    int main()
    {
        auto var = [std::make_pair](http://en.cppreference.com/w/cpp/utility/pair/make_pair)(1, [std::string](http://en.cppreference.com/w/cpp/string/basic_string){"one"});
     
        [std::cout](http://en.cppreference.com/w/cpp/io/cout) << get_val<0>(var) << " = " << get_val<1>(var);
    }

Output:

    
    
    1 = one

###  [ [ edit
](/mwiki/index.php?title=cpp/utility/pair/tuple_element&action=edit&section=4
"Edit section: Defect reports") ]  Defect reports

The following behavior-changing defect reports were applied retroactively to
previously published C++ standards.

DR  |  Applied to  |  Behavior as published  |  Correct behavior   
---|---|---|---  
[ LWG 2974 ](https://cplusplus.github.io/LWG/issue2974) |  C++11  |  out-of-bounds index referred the undefined primary template  |  made ill-formed (hard error)   
  
###  [ [ edit
](/mwiki/index.php?title=cpp/utility/pair/tuple_element&action=edit&section=5
"Edit section: See also") ]  See also

[ Structured binding ](/w/cpp/language/structured_binding "cpp/language/structured binding") (C++17)  |  binds the specified names to sub-objects or tuple elements of the initializer  [ [edit] ](https://en.cppreference.com/mwiki/index.php?title=Template:cpp/language/dsc_structured_binding&action=edit)  
---|---  
[ std::tuple_element  <std::tuple> ](/w/cpp/utility/tuple/tuple_element "cpp/utility/tuple/tuple element") (C++11)  |  obtains the type of the specified element   
(class template specialization)  [ [edit]
](https://en.cpprefer