首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::tuple_size<std::pair>

Defined in header <utility>

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_sizeFORMIS提供了一种编译时方法,可以使用类似于元组的语法获取对中的元素数(始终为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>

二次

代码语言:javascript
复制
#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));
}

二次

产出:

二次

代码语言:javascript
复制
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)

代码语言:txt
复制
 © cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券