Difference between revisions of "cpp/container/span/dynamic extent"
From cppreference.com
(+) |
m (add link to std::extents) |
||
(8 intermediate revisions by 6 users not shown) | |||
Line 2: | Line 2: | ||
{{cpp/container/span/navbar}} | {{cpp/container/span/navbar}} | ||
{{dcl begin}} | {{dcl begin}} | ||
− | {{dcl header | span}} | + | {{dcl header|span}} |
− | {{dcl | since=c++20 |1= | + | {{dcl|since=c++20|1= |
− | inline constexpr std:: | + | inline constexpr std::size_t dynamic_extent = std::numeric_limits<std::size_t>::max(); |
}} | }} | ||
{{dcl end}} | {{dcl end}} | ||
− | {{tt|std::dynamic_extent}} is a constant of type {{lc|std:: | + | {{tt|std::dynamic_extent}} is a constant of type {{lc|std::size_t}} that is generally used to indicate that any type using {{tt|std::dynamic_extent}} will ''dynamically'' store its value (e.g., size) rather than having the value ''statically'' known in the type. |
+ | |||
+ | It is being used in several contexts: | ||
+ | * To differentiate {{ltt|cpp/container/span|std::span}} of static and dynamic extent. | ||
+ | {{rrev|since=c++23|1= | ||
+ | * To indicate that the extent at a certain rank index will be stored dynamically in {{ltt|cpp/container/mdspan/extents|std::extents}}. | ||
+ | }} | ||
+ | {{rrev|since=c++26|1= | ||
+ | * To indicate that the padded layouts for {{ltt|cpp/container/mdspan|std::mdspan}} will dynamically store its padding value. | ||
+ | }} | ||
+ | |||
+ | ===Note=== | ||
+ | Since {{lc|std::size_t}} is an unsigned type, an equivalent definition is: | ||
+ | {{source|1= | ||
+ | inline constexpr std::size_t dynamic_extent = -1; | ||
+ | }} | ||
+ | |||
+ | See [[cpp/language/implicit_conversion#Integral_conversions|integral conversions]]. | ||
+ | |||
+ | ===Example=== | ||
+ | {{example | ||
+ | |code= | ||
+ | #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); | ||
+ | } | ||
+ | |output= | ||
+ | span1, static extent = 5 | ||
+ | span2, dynamic extent | ||
+ | span3, static extent = 5 | ||
+ | span4, dynamic extent | ||
+ | }} | ||
===See also=== | ===See also=== | ||
{{dsc begin}} | {{dsc begin}} | ||
− | {{dsc inc | cpp/container/dsc span}} | + | {{dsc inc|cpp/container/dsc span}} |
+ | {{dsc inc|cpp/container/mdspan/dsc extents}} | ||
{{dsc end}} | {{dsc end}} | ||
− | + | {{langlinks|de|es|ja|ru|zh}} |
Latest revision as of 21:35, 12 April 2024
Defined in header <span>
|
||
inline constexpr std::size_t dynamic_extent = std::numeric_limits<std::size_t>::max(); |
(since C++20) | |
std::dynamic_extent
is a constant of type std::size_t that is generally used to indicate that any type using std::dynamic_extent
will dynamically store its value (e.g., size) rather than having the value statically known in the type.
It is being used in several contexts:
- To differentiate std::span of static and dynamic extent.
|
(since C++23) |
|
(since C++26) |
[edit] Note
Since std::size_t is an unsigned type, an equivalent definition is:
inline constexpr std::size_t dynamic_extent = -1;
See integral conversions.
[edit] Example
Run this code
#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); }
Output:
span1, static extent = 5 span2, dynamic extent span3, static extent = 5 span4, dynamic extent
[edit] See also
(C++20) |
a non-owning view over a contiguous sequence of objects (class template) |
(C++23) |
a descriptor of a multidimensional index space of some rank (class template) |