Search⌘ K
AI Features

The Standard Library

Understand the new features of the C++20 standard library, including std::span for safer array views, container improvements like constexpr constructors, safe integer comparisons, extended chrono calendar and time zone support, and the new formatting library for safer and flexible string formatting.

std::span

A std::span represents an object that can refer to a contiguous sequence of objects. A std::span, sometimes also called a view, is never an owner. This view can be a C-array, a std::array, a pointer with a size, or a std::vector. A typical implementation of a std::span needs a pointer to its first element and a size.

The main reason for having a std::span is that a plain array will decay to a pointer if passed to a function; therefore, its size is lost. std::span automatically finds the size of an array, a std::array, or a std::vector. If you use a pointer to initialize a std::span, you have to provide the size in the constructor.

C++ 17
void copy_n(const int* src, int* des, int n){}
void copy(std::span<const int> src, std::span<int> des){}
int main(){
int arr1[] = {1, 2, 3};
int arr2[] = {3, 4, 5};
copy_n(arr1, arr2, 3);
copy(arr1, arr2);
}

Container improvements

C++20 has many improvements regarding containers of the Standard Template Library. First of all, std::vector and ...