Scroll to navigation

std::size,std::ssize(3) C++ Standard Libary std::size,std::ssize(3)

NAME

std::size,std::ssize - std::size,std::ssize

Synopsis


Defined in header <array>
Defined in header <deque>
Defined in header <forward_list>
Defined in header <iterator>
Defined in header <list>
Defined in header <map>
Defined in header <regex>
Defined in header <set>
Defined in header <span> (since C++20)
Defined in header <string>
Defined in header <string_view>
Defined in header <unordered_map>
Defined in header <unordered_set>
Defined in header <vector>
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.


1-2) Returns c.size(), converted to the return type if necessary.
3-4) Returns N.

Parameters


c - a container or view with a size member function
array - an array of arbitrary type

Return value


The size of c or array.

Exceptions


1-2) May throw implementation-defined exceptions.


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

First version


template <class C>
constexpr auto size(const C& c) -> decltype(c.size())
{
return c.size();
}

Second version


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());
}
Third version
template <class T, std::size_t N>
constexpr std::size_t size(const T (&array)[N]) noexcept
{
return N;
}
Fourth version
template <class T, std::ptrdiff_t N>
constexpr std::ptrdiff_t ssize(const T (&array)[N]) noexcept
{
return N;
}

Notes


Feature-test macro: __cpp_lib_nonmember_container_access (for std::size())


Feature-test macro: __cpp_lib_ssize (for std::ssize())

Example

// Run this code


#include <iostream>
#include <vector>


int main()
{
std::vector<int> v = { 3, 1, 4 };
std::cout << std::size(v) << '\n';


int a[] = { -5, 10, 15 };
std::cout << std::size(a) << '\n';


// since C++20 the signed size (ssize) is available
auto i = std::ssize(v);
for (--i; i != -1; --i) {
std::cout << v[i] << (i ? ' ' : '\n');
}
std::cout << "i = " << i << '\n';
}

Output:


3
3
4 1 3
i = -1

See also


ptrdiff_t signed integer type returned when subtracting two pointers
(typedef)
size_t unsigned integer type returned by the sizeof operator
(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)

2022.07.31 http://cppreference.com