std::ranges...erface::data(3)

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

πŸŽ€ std::ranges::view_interface::data πŸŽ€

okay bestie, this page is from the C++ Standard Libary manual β€” yes, that's the manual name β€” and it's all about std::ranges::view_interface::data, page (3). It's giving default data() member function energy, and we're gonna get into it. πŸ’…


🌸 NAME 🌸

std::ranges::view_interface::data β€” literally std::ranges::view_interface::data, the member function. That's the name, no cap. 🫢


✨ Synopsis ✨

Here are the two overloads, with their requires clauses doing main character work:

constexpr auto data()                                          (1)
(since C++20)
       requires std::contiguous_iterator<ranges::iterator_t<D>>;
   constexpr auto data() const

       requires ranges::range<const D> &&                         (2)
(since C++20)

           std::contiguous_iterator<ranges::iterator_t<const D>>;

So the default implementation of the data() member function obtains the address denoted by the beginning iterator via std::to_address, and that is also the lowest address of the contiguous storage (implied by contiguous_iterator) referenced by the view of the derived type when the view is not empty. Lowkey precise, highkey iconic. ✨

  1. Let derived be static_cast<D&>(*this). Equivalent to return std::to_address(ranges::begin(derived));.
  2. Same as (1), except that derived is static_cast<const D&>(*this).

πŸ’– Parameters πŸ’–

(none) β€” literally none. Zero parameters. Not a single one, bestie. 🫢


🫢 Return value 🫢

The address denoted by the beginning iterator. That's it, that's the tea. 🍡


🧚 Notes 🧚

These derived types may use the default implementation of data():

  • std::ranges::common_view
  • std::ranges::drop_view
  • std::ranges::drop_while_view
  • std::ranges::ref_view
  • std::ranges::subrange
  • std::ranges::take_view
  • std::ranges::take_while_view

And these types are derived from std::ranges::view_interface and do not declare their own data() member function, but they cannot use the default implementation, because their iterator types never satisfy contiguous_iterator:

  • std::ranges::basic_istream_view
  • std::ranges::elements_view
  • std::ranges::filter_view
  • std::ranges::iota_view
  • std::ranges::join_view
  • std::ranges::lazy_split_view
  • std::ranges::reverse_view
  • std::ranges::split_view
  • std::ranges::transform_view

πŸ“ Example πŸ“

// Run this code

#include <array>
#include <iostream>
#include <ranges>
#include <string_view>

int main() {
    constexpr std::string_view str { "Hello, C++20!" };
    std::cout << (str | std::views::drop(7)).data() << '\n';
    constexpr static std::array a { 1,2,3,4,5 };
    constexpr auto v { a | std::views::take(3) };
    static_assert( &a[0] == v.data() );
}

πŸ’œ Output πŸ’œ

C++20!

🌈 See also 🌈

  • data β€” obtains the pointer to the underlying array (C++17) (function template)
  • 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)
  • to_address β€” obtains a raw pointer from a pointer-like type (C++20) (function template)

source/version/date: http://cppreference.com Β· 2024.06.10 Β· std::ranges...erface::data(3) β€” byeee bestie, go check that contiguous storage πŸ’–βœ¨

πŸ“¦ where this page comes from