std::span::first(3)
π 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 > Extentwon't even compile (ill-formed, not your girl's problem to fix), andCount > 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
- 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