std::meta::tuple_element
From cppreference.com
| Defined in header <meta>
|
||
consteval std::meta::info tuple_element( std::size_t i, std::meta::info r );
|
(since C++26) | |
Returns a reflected type obtained by std::tuple_element<I, T>::type, where T is the tuple-like type represented by std::meta::dealias(r) and I is a constant equal to i.
If i >= std::meta::tuple_size(r), the program is ill-formed.
Parameters
| i | - | an index of the element in the tuple |
| r | - | a reflection value |
Return value
The reflected type of the tuple-like type's element accessed by index i.
Exceptions
Throws std::meta::exception if r does not represent a type or type alias.
Example
Run this code
#include <array>
#include <cstddef>
#include <iostream>
#include <meta>
#include <ranges>
#include <tuple>
#include <type_traits>
#include <utility>
template<typename T1, typename T2, typename T3>
struct Triple { T1 t1; T2 t2; T3 t3; };
// A specialization of std::tuple_element for program-defined type Triple:
template<std::size_t I, typename T1, typename T2, typename T3>
struct std::tuple_element<I, Triple<T1, T2, T3>>
{ static_assert(false, "Invalid index"); };
template<typename T1, typename T2, typename T3>
struct std::tuple_element<0, Triple<T1, T2, T3>> { using type = T1; };
template<typename T1, typename T2, typename T3>
struct std::tuple_element<1, Triple<T1, T2, T3>> { using type = T2; };
template<typename T1, typename T2, typename T3>
struct std::tuple_element<2, Triple<T1, T2, T3>> { using type = T3; };
template<typename... Args>
struct TripleTypes
{
static_assert(3 == sizeof...(Args), "Expected exactly 3 type names");
template<std::size_t N>
using type = typename [:tuple_element(N, ^^Triple<Args...>):];
};
static_assert((dealias(^^TripleTypes<char, int, float>::type<0>) == ^^char) and
(dealias(^^TripleTypes<char, int, float>::type<1>) == ^^int) and
(dealias(^^TripleTypes<char, int, float>::type<2>) == ^^float));
using Tri = Triple<int, char, short>; //< Program-defined type
static_assert((tuple_element(0, ^^Tri) == ^^int) and
(tuple_element(1, ^^Tri) == ^^char) and
(tuple_element(2, ^^Tri) == ^^short));
using Tuple = std::tuple<int, char, short>;
static_assert((tuple_element(0, ^^Tuple) == ^^int) and
(tuple_element(1, ^^Tuple) == ^^char) and
(tuple_element(2, ^^Tuple) == ^^short));
using Array3 = std::array<int, 3>;
static_assert((tuple_element(0, ^^Array3) == ^^int) and
(tuple_element(1, ^^Array3) == ^^int) and
(tuple_element(2, ^^Array3) == ^^int));
using Pair = std::pair<Tuple, Tri>;
static_assert((tuple_element(0, ^^Pair) == std::meta::dealias(^^Tuple)) and
(tuple_element(1, ^^Pair) == std::meta::dealias(^^Tri)));
using Sub = std::ranges::subrange<int*, int*>;
static_assert((tuple_element(0, ^^Sub) == ^^int*) and
(tuple_element(1, ^^Sub) == ^^int*));
int main() {}
