std::span::size(3)

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

πŸ‘‘ std::span::size πŸ‘‘

okay so bestie, this is the <std::span::size>(3) page from the C++ Standard Libary β€” yes, that’s the spelling in the header, and we’re keeping it πŸ’…. It’s giving β€œtiny member function, main character energy,” and we’re gonna get through every detail together, fr fr. ✨


πŸ’– NAME πŸ’–

std::span::size β€” literally std::span::size. That’s the name, bestie. No plot twist, no secret second identity. πŸŽ€


🌸 Synopsis 🌸

Here’s the signature, and yes it’s constexpr, const noexcept, and since C++20 β€” the whole slay package πŸ”₯:

constexpr size_type size() const noexcept;  (since C++20)

It returns the number of elements in the span. That’s the vibe. πŸ¦‹


πŸŽ€ Parameters πŸŽ€

(none) β€” not a single parameter, zero, nada, the void, and we respect it 🫢.


πŸ’œ Return value πŸ’œ

It returns the number of elements in the span β€” that’s the whole return value, bestie πŸ’–.


⭐ Note ⭐

Feature-test macro tea β˜•:

  • Feature-test macro: __cpp_lib_ssize
    • Value: 201902L
    • Std: (C++20)
    • Feature: std::ssize and unsigned std::span::size

🌈 Example 🌈

// Run this code

#include <iostream>
#include <span>

void show_sizes(std::span<const int> span)
{
    std::cout
        << span                 .size() << ' ' // 8
        << span.first(7)        .size() << ' ' // 7
        << span.first<6>()      .size() << ' ' // 6
        << span.last(5)         .size() << ' ' // 5
        << span.last<4>()       .size() << ' ' // 4
        << span.subspan(2, 3)   .size() << ' ' // 3
        << span.subspan<3, 2>() .size() << ' ' // 2
        << '\n';
}

int main()
{
    int antique_array[]{1, 2, 3, 4, 5, 6, 7, 8};
    show_sizes(antique_array);
}

Output:

8 7 6 5 4 3 2

πŸ¦‹ See also πŸ¦‹

  • constructor β€” constructs a span (public member function) πŸ› οΈ
  • size_bytes β€” returns the size of the sequence in bytes (public member function) πŸ“

later bestie! this page is from http://cppreference.com, dated 2024.06.10, and the footer says std::span::size(3) β€” mwah πŸ’‹βœ¨

πŸ“¦ where this page comes from