std::extent
來自 cppreference.com
定義於標頭檔案 <type_traits> |
||
template< class T, unsigned N = 0 > struct extent; |
(C++11 起) | |
如果 T
是一個數組型別,則提供成員常量 value
,等於陣列第 N
th 維的元素數量,如果 N
在 [
0,
std::rank<T>::value)
範圍內。對於任何其他型別,或者如果 T
是一個第一維未知邊界的陣列且 N
是 0,則 value
為 0。
如果程式為 std::extent
或 std::extent_v
(自 C++17 起) 新增特化,則行為是未定義的。
目錄 |
[編輯] 輔助變數模板
template< class T, unsigned N = 0 > constexpr std::size_t extent_v = extent<T, N>::value; |
(C++17 起) | |
繼承自 std::integral_constant
成員常量
value [靜態] |
T 的第 N 維的元素數量 (public static 成員常量) |
成員函式
operator std::size_t |
將物件轉換為 std::size_t,返回 value (公開成員函式) |
operator() (C++14) |
返回 value (公開成員函式) |
成員型別
型別 | 定義 |
value_type
|
std::size_t |
型別
|
std::integral_constant<std::size_t, value> |
[編輯] 可能實現
template<class T, unsigned N = 0> struct extent : std::integral_constant<std::size_t, 0> {}; template<class T> struct extent<T[], 0> : std::integral_constant<std::size_t, 0> {}; template<class T, unsigned N> struct extent<T[], N> : std::extent<T, N - 1> {}; template<class T, std::size_t I> struct extent<T[I], 0> : std::integral_constant<std::size_t, I> {}; template<class T, std::size_t I, unsigned N> struct extent<T[I], N> : std::extent<T, N - 1> {}; |
[編輯] 示例
執行此程式碼
#include <type_traits> static_assert( std::extent_v<int[3]> == 3 && // default dimension is 0 std::extent_v<int[3], 0> == 3 && // the same as above std::extent_v<int[3][4], 0> == 3 && std::extent_v<int[3][4], 1> == 4 && std::extent_v<int[3][4], 2> == 0 && std::extent_v<int[]> == 0 ); int main() { const auto ext = std::extent<int['*']>{}; static_assert(ext == 42); // with implicit conversion to std::size_t const int ints[]{1, 2, 3, 4}; static_assert(std::extent_v<decltype(ints)> == 4); // array size [[maybe_unused]] int ary[][3] = {{1, 2, 3}}; // ary[0] is of type reference to 'int[3]', so, the extent // cannot be calculated correctly and it returns 0 static_assert(std::is_same_v<decltype(ary[0]), int(&)[3]>); static_assert(std::extent_v<decltype(ary[0])> == 0); // removing reference gives correct extent value 3 static_assert(std::extent_v<std::remove_cvref_t<decltype(ary[0])>> == 3); }
[編輯] 參閱
(C++11) |
檢查型別是否為陣列型別 (類模板) |
(C++11) |
獲取陣列型別的維數 (類模板) |
(C++11) |
從給定陣列型別中移除一個維度 (類模板) |
(C++11) |
從給定陣列型別中移除所有維度 (類模板) |
(C++23) |
某個秩的多維索引空間的描述符 (類模板) |