std::dynamic_extent(3)
π NAME π
okay so std::dynamic_extent β yes bestie, that's literally the whole name, it's giving minimalist icon behavior β¨
πΈ SYNOPSIS πΈ
so first things first, she lives in this header:
Defined in header <span>
and here's the whole vibe, no cap:
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. like... she's the "we'll figure it out at runtime" of the standard library, and honestly? relatable π
she's being used in several contexts, and yes she's booked and busy:
- To differentiate
std::spanof static and dynamic extent. π¦ - To indicate that the extent at a certain rank index will be stored dynamically in
std::extents(since C++23) β - To indicate that the padded layouts for
std::mdspanwill dynamically store its padding value (since C++26) π₯
π NOTE π
okay tiny lore drop: since std::size_t is an unsigned type, an equivalent definition is:
inline constexpr std::size_t dynamic_extent = -1;
like, -1 wrapping around to max? iconic behavior honestly. See integral conversions. π
β¨ EXAMPLE β¨
// Run this code β go bestie, run it, you know you want to π§
#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 how span1 and span3 got their sizes locked in at compile time, and span2 and span4 said "dynamic, we'll see"? that's the whole point of her π
π SEE ALSO π
spanβ a non-owning view over a contiguous sequence of objects (C++20) (class template) πextentsβ a descriptor of a multidimensional index space of some rank (C++23) (class template) πΈ
okay that's the page babes, go forth and span responsibly π
source: http://cppreference.com β version/date: 2024.06.10 β page: std::dynamic_extent(3) β mwah, love u, bye πβ¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms