std::get(std::pair)
From cppreference.com
C++
Feature test macros (C++20)
Concepts library (C++20)
Metaprogramming library (C++11)
Ranges library (C++20)
Filesystem library (C++17)
Concurrency support library (C++11)
Execution control library (C++26)
Utilities library
Relational operators (deprecated in C++20)
Integer comparison functions
Swap and type operations
Common vocabulary types
Type support (basic types, RTTI)
Library feature-test macros (C++20)
(C++11)
(C++20)
(C++26)
(C++20)
Coroutine support (C++20)
Contract support (C++26)
(C++20)(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)(C++20)(C++20)
(C++20)(C++20)(C++20)
General utilities
(C++20)(C++20)(C++20)
(C++20)(C++20)(C++20)
(C++20)
std::pair
(C++11)
(until C++20)(until C++20)(until C++20)(until C++20)(until C++20)(C++20)
(C++11)
get(std::pair)
(C++11)
(C++11)
(C++11)
(C++23)
(C++23)
(C++11)
Deduction guides (C++17)
Defined in header
<utility>
template< std::size_t I, class T1, class T2 >
(1)
(since C++11) typename std::tuple_element <I, std::pair <T1,T2> >::type&
(constexpr since C++14)
template< std::size_t I, class T1, class T2 >
(2)
(since C++11) const typename std::tuple_element <I, std::pair <T1,T2> >::type&
(constexpr since C++14)
template< std::size_t I, class T1, class T2 >
(3)
(since C++11) typename std::tuple_element <I, std::pair <T1,T2> >::type&&
(constexpr since C++14)
template< std::size_t I, class T1, class T2 >
(4)
(since C++11) const typename std::tuple_element <I, std::pair <T1,T2> >::type&&
(constexpr since C++14)
template< class T, class U >
constexpr T& get( std::pair <T, U>& p ) noexcept;
(5)
(since C++14)
constexpr T& get( std::pair <T, U>& p ) noexcept;
template< class T, class U >
constexpr const T& get( const std::pair <T, U>& p ) noexcept;
(6)
(since C++14)
constexpr const T& get( const std::pair <T, U>& p ) noexcept;
template< class T, class U >
constexpr T&& get( std::pair <T, U>&& p ) noexcept;
(7)
(since C++14)
constexpr T&& get( std::pair <T, U>&& p ) noexcept;
template< class T, class U >
constexpr const T&& get( const std::pair <T, U>&& p ) noexcept;
(8)
(since C++14)
constexpr const T&& get( const std::pair <T, U>&& p ) noexcept;
template< class T, class U >
constexpr T& get( std::pair <U, T>& p ) noexcept;
(9)
(since C++14)
constexpr T& get( std::pair <U, T>& p ) noexcept;
template< class T, class U >
constexpr const T& get( const std::pair <U, T>& p ) noexcept;
(10)
(since C++14)
constexpr const T& get( const std::pair <U, T>& p ) noexcept;
template< class T, class U >
constexpr T&& get( std::pair <U, T>&& p ) noexcept;
(11)
(since C++14)
constexpr T&& get( std::pair <U, T>&& p ) noexcept;
template< class T, class U >
constexpr const T&& get( const std::pair <U, T>&& p ) noexcept;
(12)
(since C++14)
constexpr const T&& get( const std::pair <U, T>&& p ) noexcept;
Extracts an element from the pair using tuple-like interface.
1-4) The index-based overloads fail to compile if the index
I
is neither 0 nor 1.5-12) The type-based overloads fail to compile if the types
T
and U
are the same.[edit] Parameters
p
-
pair whose contents to extract
[edit] Return value
1-4) Returns a reference to p.first if I == 0 and a reference to p.second if I == 1.
5-8) Returns a reference to p.first.
9-12) Returns a reference to p.second.
[edit] Example
Run this code
#include <iostream> #include <utility> int main() { auto p = std::make_pair (1, 3.14); std::cout << '(' << std::get<0>(p) << ", " << std::get<1>(p) << ")\n"; std::cout << '(' << std::get<int>(p) << ", " << std::get<double>(p) << ")\n"; }
Output:
(1, 3.14) (1, 3.14)
[edit] 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 2485 | C++11 (by index) C++14 (by type) |
there are no overloads for const pair&& | the overloads are added |
[edit] See also
Structured binding (C++17)
binds the specified names to sub-objects or tuple elements of the initializer[edit]
(C++17)
(function template) [edit]
(C++26)
(function template) [edit]