GSP
Quick Navigator

Search Site

Unix VPS
A - Starter
B - Basic
C - Preferred
D - Commercial
MPS - Dedicated
Previous VPSs
* Sign Up! *

Support
Contact Us
Online Help
Handbooks
Domain Status
Man Pages

FAQ
Virtual Servers
Pricing
Billing
Technical

Network
Facilities
Connectivity
Topology Map

Miscellaneous
Server Agreement
Year 2038
Credits
 

USA Flag

 

 

Man Pages
std::tuple_element(3) C++ Standard Libary std::tuple_element(3)

std::tuple_element - std::tuple_element


Defined in header <utility>
template< std::size_t I, class T1, class T2 > (since C++11)
struct tuple_element<I, std::pair<T1, T2>>;


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 type Definition
type T1 if I == 0
T2 if I == 1


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

// Run this code


#include <tuple>
#include <iostream>
#include <string>


namespace detail {


template <std::size_t>
struct index_tag { explicit constexpr 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);
}


1 = one


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 C++11 out-of-bounds index referred the undefined made ill-formed (hard
primary template error)


Structured binding (C++17) binds the specified names to sub-objects
or tuple elements of the initializer
std::tuple_element<std::tuple> obtains the type of the specified element
(C++11) (class template specialization)
std::tuple_element<std::array> obtains the type of the elements of array
(C++11) (class template specialization)
std::tuple_element<std::ranges::subrange> obtains the type of the iterator or the
(C++20) sentinel of a std::ranges::subrange
(class template specialization)
std::tuple_size<std::pair> obtains the size of a pair
(C++11) (class template specialization)

2022.07.31 http://cppreference.com

Search for    or go to Top of page |  Section 3 |  Main Index

Powered by GSP Visit the GSP FreeBSD Man Page Interface.
Output converted with ManDoc.