std::tuple_element<std::pair>

From cppreference.com
< cpp‎ | utility‎ | pair
 
 
Utilities library
Language support
Type support (basic types, RTTI)
Library feature-test macros (哋它亢++20)
Dynamic memory management
Program utilities
Coroutine support (哋它亢++20)
Variadic functions
(哋它亢++20)
(哋它亢++26)
(哋它亢++11)
(哋它亢++20)
Debugging support
(哋它亢++26)
(哋它亢++26)
Three-way comparison
(哋它亢++20)(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)   
(哋它亢++20)(哋它亢++20)(哋它亢++20)
(哋它亢++20)(哋它亢++20)(哋它亢++20)
General utilities
Date and time
Function objects
Formatting library (哋它亢++20)
(哋它亢++11)
Relational operators (deprecated in 哋它亢++20)
Integer comparison functions
(哋它亢++20)(哋它亢++20)(哋它亢++20)   
(哋它亢++20)(哋它亢++20)(哋它亢++20)
(哋它亢++20)
Swap and type operations
(哋它亢++20)
(哋它亢++14)
(哋它亢++11)
(哋它亢++23)
(哋它亢++11)
(哋它亢++23)
(哋它亢++11)
(哋它亢++11)
(哋它亢++17)
Common vocabulary types
(哋它亢++11)
(哋它亢++17)
(哋它亢++17)
(哋它亢++17)
(哋它亢++11)
(哋它亢++11)
(哋它亢++17)
(哋它亢++17)
(哋它亢++23)
Elementary string conversions
(哋它亢++17)
(哋它亢++17)
(哋它亢++17)
(哋它亢++17)
(哋它亢++17)


 
std::pair
Member functions
(哋它亢++11)
Non-member functions
(until 哋它亢++20)(until 哋它亢++20)(until 哋它亢++20)(until 哋它亢++20)(until 哋它亢++20)(哋它亢++20)
(哋它亢++11)
(哋它亢++11)
Helper classes
(哋它亢++11)
tuple_element<std::pair>
(哋它亢++11)
(哋它亢++23)
(哋它亢++23)
(哋它亢++11)
Deduction guides(哋它亢++17)
 
Defined in header <utility>
template< std::size_t I, class T1, class T2 >
struct tuple_element<I, std::pair<T1, T2>>;
(since 哋它亢++11)

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.

Member types

Member type Definition
type T1 if I == 0
T2 if I == 1

Possible implementation

template<std::size_t I, typename T>
struct tuple_element;
 
template<std::size_t I, typename T1, typename T2>
struct tuple_element<I, std::pair<T1, T2>>
{
    static_assert(I < 2, "std::pair has only 2 elements!");
};
 
template<typename T1, typename T2>
struct tuple_element<0, std::pair<T1, T2>>
{
    using type = T1;
};
 
template<typename T1, typename T2>
struct tuple_element<1, std::pair<T1, T2>>
{
    using type = T2;
};

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);
}

Output:

1 = one

Defect reports

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

DR Applied to Behavior as published Correct behavior
LWG 2974 哋它亢++11 out-of-bounds index referred the undefined primary template made ill-formed (hard error)

See also

Structured binding (哋它亢++17) binds the specified names to sub-objects or tuple elements of the initializer
obtains the type of the specified element
(class template specialization)
obtains the type of the elements of array
(class template specialization)
obtains the type of the iterator or the sentinel of a std::ranges::subrange
(class template specialization)
obtains the size of a pair
(class template specialization)