std::tuple_element<std::tuple>
Min standard notice:
Header: <tuple>
Provides compile-time indexed access to the types of the elements of the tuple.
# Declarations
template< std::size_t I, class... Types >
struct tuple_element< I, std::tuple<Types...> >;
(since C++11)
# Example
#include <boost/type_index.hpp>
#include <cstddef>
#include <iostream>
#include <string>
#include <tuple>
#include <utility>
template<typename TupleLike, std::size_t I = 0>
void printTypes()
{
if constexpr (I == 0)
std::cout << boost::typeindex::type_id_with_cvr<TupleLike>() << '\n';
if constexpr (I < std::tuple_size_v<TupleLike>)
{
using SelectedType = std::tuple_element_t<I, TupleLike>;
std::cout << " The type at index " << I << " is: "
<< boost::typeindex::type_id_with_cvr<SelectedType>() << '\n';
printTypes<TupleLike, I + 1>();
}
}
struct MyStruct {};
using MyTuple = std::tuple<int, long&, const char&, bool&&,
std::string, volatile MyStruct>;
using MyPair = std::pair<char, bool&&>;
static_assert(std::is_same_v<std::tuple_element_t<0, MyPair>, char>);
static_assert(std::is_same_v<std::tuple_element_t<1, MyPair>, bool&&>);
int main()
{
printTypes<MyTuple>();
printTypes<MyPair>();
}