std::span::first(3)

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

πŸ’… NAME πŸ’…

std::span::first β€” okay so it's literally named std::span::first, bestie, that's the whole thing πŸŽ€ and the manual page we're vibing in is std::span::first(3) from the C++ Standard Library. She's iconic, she's a member function, let's go ✨


🌸 Synopsis 🌸

template< std::size_t Count >
(since C++20)
constexpr std::span<element_type, Count> first() const;
constexpr std::span<element_type, std::dynamic_extent> first(
(since C++20)
size_type Count ) const;

So we got two overloads, both const, both constexpr, both since C++20 (yes queen, C++20 era πŸ’œ).

The first one is the template baddie: you hand it Count as a template parameter, and she gives you back std::span<element_type, Count> β€” a fixed-extent span, periodt.

The second one takes a runtime size_type Count argument and gives you std::span<element_type, std::dynamic_extent> β€” dynamic extent energy, main character vibes πŸ¦‹


πŸ‘‘ DESCRIPTION πŸ‘‘

Lowkey what this does: it obtains a span that is a view over the first Count elements of this span. That's it. That's the whole flex πŸ’…

Now the serious part, bestie, listen up 🫢:

  • The program is ill-formed if Count > Extent.
  • The behavior is undefined if Count > size().
🚨 No cap, those two caveats are the whole personality. Count > Extent won't even compile (ill-formed, not your girl's problem to fix), and Count > size() is undefined behavior β€” like, actual "we don't know what happens, don't do this" undefined. Respect the bounds or perish πŸ˜­πŸ’€

⭐ Return value ⭐

You get back a span r that is a view over the first Count elements of *this, such that:

r.data() == this->data() && r.size() == Count

So the data pointer stays exactly where it was (same start, no shifting, no copying πŸ”₯) and the size is literally just Count. Clean. Minimal. She ate πŸ“


🧚 Example 🧚

// Run this code

#include <iostream>
#include <ranges>
#include <span>
#include <string_view>

void print(std::string_view const title,
           std::ranges::forward_range auto const& container)
{
    auto size{std::size(container)};
    std::cout << title << '[' << size << "]{";
    for (auto const& elem : container)
        std::cout << elem << (--size ? ", " : "");
    std::cout << "};\n";
}

void run_game(std::span<const int> span)
{
    print("span: ", span);

    std::span<const int, 5> span_first = span.first<5>();
    print("span.first<5>(): ", span_first);

    std::span<const int, std::dynamic_extent> span_first_dynamic =
span.first(4);
    print("span.first(4): ", span_first_dynamic);
}

int main()
{
    int a[8]{1, 2, 3, 4, 5, 6, 7, 8};
    print("int a", a);
    run_game(a);
}

πŸ¦‹ Output πŸ¦‹

int a[8]{1, 2, 3, 4, 5, 6, 7, 8};
span: [8]{1, 2, 3, 4, 5, 6, 7, 8};
span.first<5>(): [5]{1, 2, 3, 4, 5};
span.first(4): [4]{1, 2, 3, 4};

See how the fixed one gives you [5] elements and the dynamic one gives you [4]? Both just slice off the front and keep the same starting data. It's giving ✨effortless✨ 🫢


🌈 See also 🌈

  • last β€” obtains a subspan consisting of the last N elements of the sequence (public member function)
  • subspan β€” obtains a subspan (public member function)

iykyk, first, last and subspan are the trio, the holy trinity of span slicing πŸ‘‘πŸ’œ


✨ that's the whole page, bestie ✨ brought to you by http://cppreference.com, dated 2024.06.10, and filed under std::span::first(3) β€” mwah, go forth and definitely check your bounds first πŸ’–πŸŽ€πŸ¦‹

πŸ“¦ where this page comes from