A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://en.cppreference.com/w/cpp/language/../ranges/data.html below:

std::ranges::data - cppreference.com

inline namespace /* unspecified */ {

    inline constexpr /* unspecified */ data = /* unspecified */;

}
(since C++20)
(customization point object)

Call signature

(since C++20)

Returns a pointer to the first element of a contiguous range.

If T is an array type and std::remove_all_extents_t<std::remove_reference_t<T>> is incomplete, then the call to ranges::data is ill-formed, no diagnostic required.

If the argument is an lvalue or ranges::enable_borrowed_range<std::remove_cv_t<T>> is true, a call to ranges::data is expression-equivalent to:

  1. decay-copy(t.data())(until C++23)auto(t.data())(since C++23), if that expression is valid and its type is a pointer to an object type.
  2. Otherwise, std::to_address(ranges::begin(t)), if the expression ranges::begin(t) is valid and its type models std::contiguous_iterator.

In all other cases, a call to ranges::data is ill-formed, which can result in substitution failure when ranges::data(e) appears in the immediate context of a template instantiation.

Customization point objects

The name ranges::data denotes a customization point object, which is a const function object of a literal semiregular class type. See CustomizationPointObject for details.

[edit] Notes

If the argument is an rvalue (i.e. T is an object type) and ranges::enable_borrowed_range<std::remove_cv_t<T>> is false, the call to ranges::data is ill-formed, which also results in substitution failure.

If ranges::data(e) is valid for an expression e, then it returns a pointer to an object.

The C++20 standard requires that if the underlying data function call returns a prvalue, the return value is move-constructed from the materialized temporary object. All implementations directly return the prvalue instead. The requirement is corrected by the post-C++20 proposal P0849R8 to match the implementations.

[edit] Example
#include <cstring>
#include <iostream>
#include <ranges>
#include <string>
 
int main()
{
    std::string s{"Hello world!\n"};
 
    char a[20]; // storage for a C-style string
    std::strcpy(a, std::ranges::data(s));
    // [data(s), data(s) + size(s)] is guaranteed to be an NTBS
 
    std::cout << a;
}

Output:

[edit] See also obtains a pointer to the beginning of a read-only contiguous range
(customization point object)[edit] returns an iterator to the beginning of a range
(customization point object)[edit] obtains the pointer to the underlying array
(function template) [edit]

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4