std::span<T,Extent>::size_bytes

来自cppreference.com
< cpp‎ | container‎ | span
constexpr size_type size_bytes() const noexcept;
(C++20 起)

返回以字节计的序列大小。

返回值

size() * sizeof(element_type)

示例

#include <cstdint>
#include <iostream>
#include <span>
 
constexpr static std::int32_t a[]{1, 2, 3, 4, 5};
constexpr static std::span s{a};
 
static_assert
(
    sizeof(int32_t) == 4 &&
    std::size(a) == 5 &&
    sizeof a == 20 &&
    s.size() == 5 &&
    s.size_bytes() == 20
);
 
int main()
{
    // 静态 span 通常仅持有一个指针:
    std::cout << sizeof(s) << '\n';
}

可能的输出:

8

参阅

(C++20)
返回序列中的元素数
(公开成员函数)