std::ranges...erface::data(3)
π 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. β¨
- Let
derivedbestatic_cast<D&>(*this). Equivalent toreturn std::to_address(ranges::begin(derived));. - Same as (1), except that
derivedisstatic_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_viewstd::ranges::drop_viewstd::ranges::drop_while_viewstd::ranges::ref_viewstd::ranges::subrangestd::ranges::take_viewstd::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_viewstd::ranges::elements_viewstd::ranges::filter_viewstd::ranges::iota_viewstd::ranges::join_viewstd::ranges::lazy_split_viewstd::ranges::reverse_viewstd::ranges::split_viewstd::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
- 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