std::dynamic_extent(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’œ 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::span of 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::mdspan will 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