std::dynamic_extent
來自 cppreference.com
在標頭檔案 <span> 中定義 |
||
inline constexpr std::size_t dynamic_extent = std::numeric_limits<std::size_t>::max(); |
(C++20 起) | |
std::dynamic_extent
是一個 std::size_t 型別的常量,通常用於指示任何使用 std::dynamic_extent
的型別將動態地儲存其值(例如,大小),而不是在型別中靜態地已知該值。
它在以下幾個上下文中使用:
- 用於區分靜態和動態範圍的 std::span。
|
(C++23 起) |
|
(C++26 起) |
[編輯] 注意
由於 std::size_t 是一個無符號型別,因此等效的定義是
inline constexpr std::size_t dynamic_extent = -1;
參見 整數轉換。
[編輯] 示例
執行此程式碼
#include <array> #include <cassert> #include <cstddef> #include <iostream> #include <span> #include <string_view> #include <vector> int main() { auto print = [](std::string_view const name, std::size_t ex) { std::cout << name << ", "; if (std::dynamic_extent == ex) std::cout << "dynamic extent\n"; else std::cout << "static extent = " << ex << '\n'; }; int a[]{1, 2, 3, 4, 5}; std::span span1{a}; print("span1", span1.extent); std::span<int, std::dynamic_extent> span2{a}; print("span2", span2.extent); std::array ar{1, 2, 3, 4, 5}; std::span span3{ar}; print("span3", span3.extent); std::vector v{1, 2, 3, 4, 5}; std::span span4{v}; print("span4", span4.extent); }
輸出
span1, static extent = 5 span2, dynamic extent span3, static extent = 5 span4, dynamic extent
[編輯] 另請參見
(C++20) |
一個連續物件序列的非擁有檢視 (類模板) |
(C++23) |
某個秩的多維索引空間的描述符 (類模板) |