std::data
Min standard notice:
Header: <array>
Returns a pointer to the block of memory containing the elements of the range.
# Declarations
template< class C >
constexpr auto data( C& c ) -> decltype(c.data());
(since C++17)
template< class C >
constexpr auto data( const C& c ) -> decltype(c.data());
(since C++17)
template< class T, std::size_t N >
constexpr T* data( T (&array)[N] ) noexcept;
(since C++17)
template< class E >
constexpr const E* data( std::initializer_list<E> il ) noexcept;
(since C++17)
# Parameters
c: a container or view with a data() member functionarray: an array of arbitrary typeil: an std::initializer_list
# Notes
The overload for std::initializer_list is necessary because it does not have a member function data.
# Example
#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;
}