std::tuple_element<std::pair>

Header: <utility>

The partial specializations of std::tuple_element for pairs provide compile-time access to the types of the pair’s elements, using tuple-like syntax. The program is ill-formed if I >= 2.

# Declarations

template< std::size_t I, class T1, class T2 >
struct tuple_element<I, std::pair<T1, T2>>;

(since C++11)

# Example

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

# Defect reports

DRApplied toBehavior as publishedCorrect behavior
LWG 2974C++11out-of-bounds index referred the undefined primary templatemade ill-formed (hard error)

# See also