std::rank
Определено в заголовке <type_traits> | ||
|---|---|---|
template< class T > struct rank; | (с C++11) |
Если T — тип массива, предоставляет статическую константу value, равную числу измерений массива. Для любого другого типа value равно 0.
Поведение программы, добавляющей специализации для std::rank или std::rank_v(с C++17) не определено.
Вспомогательный шаблон переменной
template< class T > inline constexpr std::size_t rank_v = rank<T>::value; | (с C++17) |
Наследуется от std::integral_constant
Статические константы
| value
[static] | число измерений T или ноль (публичная статическая константа) |
Члены-функции
| 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> |
Возможная реализация
template<class T>
struct rank : public std::integral_constant<std::size_t, 0> {};
template<class T>
struct rank<T[]> : public std::integral_constant<std::size_t, rank<T>::value + 1> {};
template<class T, std::size_t N>
struct rank<T[N]> : public std::integral_constant<std::size_t, rank<T>::value + 1> {}; |
Пример
#include <type_traits>
int main()
{
static_assert(
std::rank<int>{} == 0
&& std::rank<int[5]>{} == 1
&& std::rank<int[5][5]>{} == 2
&& std::rank<int[][5][5]>{} == 3 );
[[maybe_unused]] int ary[][3] = {{1, 2, 3}};
// The reason of rank of "ary[0]" is calculated as 0
static_assert(std::rank_v<decltype(ary[0])> == 0);
// is that rank cannot deal with reference type. i.e. int(&)[3]
static_assert(std::is_same_v<decltype(ary[0]), int(&)[3]>);
// The solution is to remove reference type
static_assert(std::rank_v<std::remove_cvref_t<decltype(ary[0])>> == 1);
}См. также
|
(C++11) | проверяет, является ли тип массивом (шаблон класса) |
|
(C++11) | получает размер типа массива вдоль указанного измерения (шаблон класса) |
|
(C++11) | удаляет один размер массива из заданного типа (шаблон класса) |
|
(C++11) | удаляет все размеры массива из заданного типа (шаблон класса) |
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/w/cpp/types/rank