std::data(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’– 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) β€” returns c.data().
  • 3) β€” returns array.
  • 4) β€” returns il.begin().

πŸŽ€ Parameters πŸŽ€

  • c β€” a container or view with a data() member function
  • array β€” an array of arbitrary type
  • il β€” an std::initializer_list

🌸 Return value 🌸

  • 1,2) β€” c.data()
  • 3) β€” array
  • 4) β€” 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 β€” value 201411L, std (C++17), feature std::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