std::tuple_element<std::array>
来自cppreference.com
<tbody>
</tbody>
| 在标头 <array> 定义
|
||
template< std::size_t I, class T, std::size_t N > struct tuple_element< I, std::array<T, N> >; |
(C++11 起) | |
使用 tuple 式接口,提供 array 元素类型的编译时索引访问
成员类型
| 成员类型 | 定义 |
| type | array 的元素类型 |
可能的实现
template<std::size_t I, class T>
struct tuple_element;
template<std::size_t I, class T, std::size_t N>
struct tuple_element<I, std::array<T,N>>
{
using type = T;
};
|
示例
运行此代码
#include <array>
#include <tuple>
#include <type_traits>
int main()
{
// 定义 array 并获取位于位置 0 的元素类型
std::array<int, 10> data{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
using T = std::tuple_element<0, decltype(data)>::type; // int
static_assert(std::is_same_v<T, int>);
const auto const_data = data;
using CT = std::tuple_element<0, decltype(const_data)>::type; // const int
// tuple_element 的结果取决于 tuple 式类型的 cv 限定
static_assert(!std::is_same_v<T, CT>);
static_assert(std::is_same_v<CT, const int>);
}
参阅
| 结构化绑定 (C++17) | 绑定指定的名字到初始化式的子对象或元组元素 |
| 获得指定元素的类型 (类模板特化) | |
获得 pair 中元素的类型 (类模板特化) |