CppDS.com

C++ 98 11 14 17 20 手册

std::tuple_size(std::array)

来自cppreference.com
< cpp‎ | container‎ | array
定义于头文件 <array>
template< class T, std::size_t N >

struct tuple_size< std::array<T, N> > :
    std::integral_constant<std::size_t, N>

{ };
(C++11 起)

提供作为编译时常量表达式访问 std::array 中元素数量的方法。

继承自 std::integral_constant

成员常量

value
[静态]
N , array 中的元素数
(公开静态成员常量)

成员函数

operator std::size_t
转换对象为 std::size_t ,返回 value
(公开成员函数)
operator()
(C++14)
返回 value
(公开成员函数)

成员类型

 
类型 定义
value_type std::size_t
type std::integral_constant<std::size_t, value>

示例

#include <iostream>
#include <array>
 
template<class T>
void test(T t)
{
    int a[std::tuple_size<T>::value]; // 能用于编译时
    std::cout << std::tuple_size<T>::value << '\n';
}
 
int main()
{
    std::array<float, 3> arr;
    test(arr);
}

输出:

3

参阅

在编译时获得 tuple 的大小
(类模板特化)
关闭