function template
<utility>
std::get (pair)
lvalue (1) | template <size_t I, class T1, class T2> typename tuple_element< I, pair<T1,T2> >::type& get (pair<T1,T2>& pr) noexcept; |
---|
rvalue (2) | template <size_t I, class T1, class T2> typename tuple_element< I, pair<T1,T2> >::type&& get (pair<T1,T2>&& pr) noexcept; |
---|
const (3) | template <size_t I, class T1, class T2> const typename tuple_element< I, pair<T1,T2> >::type& get (const pair<T1,T2>& pr) noexcept; |
---|
Get element (tuple interface)
Returns a reference to member first if I is 0
, or a reference to member second if I is 1
.
This overload of tuple's homonym function get is provided so that pair objects can be treated as a tuples. For that purpose, header <utility>
also overloads tuple_size and tuple_element types with the appropriate members defined.
Template parameters
- I
- Position of an element in the pair, with
0
identifying member first, and 1
identifying member second.
size_t is an unsigned integral type.
- T1, T2
- Type of the elements in the pair.
Function parameters
- pr
- A pair object.
Return value
A reference to a member of the pair.
For rvalue pair objects (2), the function returns an rvalue reference (as if forward was used).
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
// accessing pairs with get
#include <utility> // std::pair, std::get
#include <iostream> // std::cout
int main () {
std::pair <int,char> foo (10,'x');
std::get<0>(foo) = 50;
std::cout << "foo contains: ";
std::cout << std::get<0>(foo) << " and " << std::get<1>(foo) << '\n';
return 0;
}
|
Output:
Data races
One of the members of pr is potentially accessed or modified by the caller. Concurrently accessing the other is safe.
Exception safety
No-throw guarantee: this function never throws exceptions.