std::size,std::ssize(3)
π NAME π
The NAME line is literally std::size,std::ssize - std::size,std::ssize β yes bestie, it's giving "I contain multitudes" energy π
. This is the std::size,std::ssize(3) page from the C++ Standard Libary manual (that's the spelling up top, we are not correcting it, periodt π).
β¨ Synopsis β¨
Okay so first things first: this icon lives in a lot of headers. Like, a lot a lot. Defined in header:
<array><deque><flat_map><flat_set><forward_list><iterator><list><map><regex><set><span><string><string_view><unordered_map><unordered_set><vector>
Here's the whole synopsis vibe, no cap:
template< class C >
(1) (since C++17)
constexpr auto size( const C& c ) -> decltype(c.size());
template< class C >
constexpr auto ssize( const C& c )
(2) (since C++20)
-> std::common_type_t<std::ptrdiff_t,
std::make_signed_t<decltype(c.size())>>;
template< class T, std::size_t N >
(3) (since C++17)
constexpr std::size_t size( const T (&array)[N] ) noexcept;
template< class T, std::ptrdiff_t N >
(4) (since C++20)
constexpr std::ptrdiff_t ssize( const T (&array)[N] ) noexcept;
Returns the size of the given range. That's the whole job, and honestly? Slay.
1,2) Returns c.size(), converted to the return type if necessary.
3,4) Returns N.
πΈ Parameters πΈ
cβ a container or view with asizemember function. It's giving "I have asize()and I'm not afraid to use it" β¨.arrayβ an array of arbitrary type. Any type, bestie, we love range.
π Return value π
- 1)
c.size() - 2)
static_cast<std::common_type_t<std::ptrdiff_t, std::make_signed_t<decltype(c.size())>>>(c.size()) - 3,4)
N
β οΈ Exceptions β οΈ
- 1,2) May throw implementation-defined exceptions. That's the whole warning, so keep it in mind π«Ά.
π« Overloads π«
Custom overloads of size may be provided for classes and enumerations that do not expose a suitable size() member function, yet can be detected.
Overloads of size found by argument-dependent lookup can be used to customize the behavior of std::ranges::size, std::ranges::ssize, and (since C++20) std::ranges::empty.
π οΈ Possible implementation π οΈ
size (1)
template<class C>
constexpr auto size(const C& c) -> decltype(c.size())
{
return c.size();
}
ssize (2)
template<class C>
constexpr auto ssize(const C& c)
-> std::common_type_t<std::ptrdiff_t,
std::make_signed_t<decltype(c.size())>>
{
using R = std::common_type_t<std::ptrdiff_t,
std::make_signed_t<decltype(c.size())>>;
return static_cast<R>(c.size());
}
size (3)
template<class T, std::size_t N>
constexpr std::size_t size(const T (&array)[N]) noexcept
{
return N;
}
ssize (4)
template<class T, std::ptrdiff_t N>
constexpr std::ptrdiff_t ssize(const T (&array)[N]) noexcept
{
return N;
}
π Notes π
Feature-test macro tea β:
__cpp_lib_nonmember_container_accessβ value201411L, Std(C++17), featurestd::size(),std::dataandstd::empty__cpp_lib_ssizeβ value201902L, Std(C++20), featurestd::ssize()(2,4)and unsignedstd::span::size()
π» Example π»
// Run this code
#include <cassert>
#include <cstring>
#include <iostream>
#include <vector>
int main()
{
// Works with containers
std::vector<int> v{3, 1, 4};
assert(std::size(v) == 3);
// And works with built-in arrays too
int a[]{-5, 10, 15};
// Returns the number of elements (not bytes) as opposed to sizeof
assert(std::size(a) == 3);
std::cout << "size of a[]: " << sizeof a << '\n'; // 12, if
sizeof(int) == 4
// Provides a safe way (compared to sizeof) of getting string
buffer size
const char str[] = "12345";
// These are fine and give the correct result
assert(std::size(str) == 6);
assert(sizeof(str) == 6);
// But use of sizeof here is a common source of bugs
const char* str_decayed = "12345";
// std::cout << std::size(str_decayed) << '\n'; // Usefully fails
to compile
std::cout << sizeof(str_decayed) << '\n'; // Prints the size of the
pointer!
// Since C++20 the signed size (std::ssize) is available
auto i = std::ssize(v);
for (--i; i != -1; --i)
std::cout << v[i] << (i ? ' ' : '\n');
assert(i == -1);
// Note that the string literal includes the ending null character,
which
// will be part of the constructed characters array. This makes
std::size
// behave differently from std::strlen and std::string::size:
constexpr char symbols[] = "0123456789";
static_assert(std::size(symbols) == 11);
static_assert(std::string(symbols).size() == 10);
assert(std::strlen(symbols) == 10);
}
π¨οΈ Possible output π¨οΈ
size of a[]: 12
8
4 1 3
π See also π
ptrdiff_tβ signed integer type returned when subtracting two pointers (typedef)size_tβ unsigned integer type returned by thesizeofoperator (typedef)ranges::sizeβ returns an integer equal to the size of a range (C++20) (customization point object)ranges::ssizeβ returns a signed integer equal to the size of a range (C++20) (customization point object)
Source: http://cppreference.com Β· Version/date: 2024.06.10 Β· Page: std::size,std::ssize(3) β bye bestie, go get that size, signed or unsigned, and slay π
β¨π¦
π¦ 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