std::data(3)
π NAME π
std::data β literally std::data, bestie. That's the name.
Okay so this is std::data from the C++ Standard Libary manual (std::data(3)), and it's literally the function template crew that returns a pointer to the block of memory containing the elements of the range. Periodt.
β¨ Synopsis β¨
Defined in header tea, all of these: <array>, <deque>, <flat_map>, <flat_set>, <forward_list>, <iterator>, <list>, <map>, <regex>, <set>, <span>, <string>, <string_view>, <unordered_map>, <unordered_set>, <vector>. That's a lot of headers, and every single one is invited to the function-template party π
template< class C > (1)
(since C++17)
constexpr auto data( C& c ) -> decltype(c.data());
template< class C > (2)
(since C++17)
constexpr auto data( const C& c ) -> decltype(c.data());
template< class T, std::size_t N > (3)
(since C++17)
constexpr T* data( T (&array)[N] ) noexcept;
template< class E > (4)
(since C++17)
constexpr const E* data( std::initializer_list<E> il ) noexcept;
Returns a pointer to the block of memory containing the elements of the range, bestie. The overload tea is:
1,2)β returnsc.data().3)β returnsarray.4)β returnsil.begin().
π Parameters π
cβ a container or view with adata()member functionarrayβ an array of arbitrary typeilβ anstd::initializer_list
πΈ Return value πΈ
1,2)βc.data()3)βarray4)βil.begin()
π¦ Exceptions π¦
1) may throw implementation-defined exceptions.
β Notes β
The overload for std::initializer_list is necessary because it does not have a member function data. Like, it literally can't do the member-function thing, so this overload exists to cover it π
Feature-test macro check, iykyk:
__cpp_lib_nonmember_container_accessβ value201411L, std(C++17), featurestd::size(), std::data(), and std::empty()
π₯ Possible implementation π₯
First version
template<class C>
constexpr auto data(C& c) -> decltype(c.data())
{
return c.data();
}
Second version
template<class C>
constexpr auto data(const C& c) -> decltype(c.data())
{
return c.data();
}
Third version
template<class T, std::size_t N>
constexpr T* data(T (&array)[N]) noexcept
{
return array;
}
Fourth version
template<class E>
constexpr const E* data(std::initializer_list<E> il) noexcept
{
return il.begin();
}
π Example π
The original has the little // Run this code note, so here's the whole vibe:
#include <cstring>
#include <iostream>
#include <string>
int main()
{
std::string s{"Hello world!\n"};
char a[20]; // storage for a C-style string
std::strcpy(a, std::data(s));
// [s.data(), s.data() + s.size()] is guaranteed to be an NTBS since C++11
std::cout << a;
}
π Output π
Hello world!
π See also π
ranges::dataβ obtains a pointer to the beginning of a contiguous range,(C++20)(customization point object)ranges::cdataβ obtains a pointer to the beginning of a read-only contiguous range,(C++20)(customization point object)
That's the whole page, bestie! Source/version/date sign-off: http://cppreference.com, 2024.06.10, page std::data(3). Mwah π
π¦ 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