std::tuple_size<std::pair>
STD::元组[医]大小<std::ping>
Defined in header | | |
---|---|---|
template< class T1, class T2 > struct tuple_size<std::pair<T1, T2>>; | | (since C++11) (until C++14) |
template <class T1, class T2> struct tuple_size<std::pair<T1, T2>> : std::integral_constant<std::size_t, 2> { }; | | (since C++14) |
部分专业化std::tuple_size
FORMIS提供了一种编译时方法,可以使用类似于元组的语法获取对中的元素数(始终为2)。
继承自STD:积分[医]常量
成员常数
value static | the constant value 2 (public static member constant) |
---|
成员函数
operator std::size_t | converts the object to std::size_t, returns value (public member function) |
---|---|
operator() (C++14) | returns value (public member function) |
成员类型
Type | Definition |
---|---|
value_type | std::size_t |
type | std::integral_constant<std::size_t, value> |
例
二次
#include <iostream>
#include <utility>
#include <tuple>
template<class T>
void test(T t)
{
int a[std::tuple_size<T>::value]; // can be used at compile time
std::cout << std::tuple_size<T>::value << '\n'; // or at run time
}
int main()
{
test(std::make_tuple(1, 2, 3.14)
test(std::make_pair(1, 3.14)
}
二次
产出:
二次
3
2
二次
另见
tuple_size | obtains the size of tuple at compile time (class template specialization) |
---|---|
std::tuple_size<std::array> | obtains the size of an array (class template specialization) |
std::tuple_element<std::pair> (C++11) | obtains the type of the elements of pair (class template specialization) |
© cppreference.com
在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。