std::ranges::data
Defined in header <ranges>
|
||
inline namespace /*unspecified*/ { inline constexpr /*unspecified*/ data = /*unspecified*/; |
(since C++20) (customization point object) |
|
Call signature |
||
template< class T > requires /* see below */ |
||
Returns a pointer to the first element of a contiguous range.
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:
- std::forward<T>(t).data(), if that expression is valid, and its return type is a pointer to an object type.
- Otherwise, std::to_address(ranges::begin(std::forward<T>(t))), if ranges::begin(std::forward<T>(t)) is valid and returns a type that models std::contiguous_iterator.
- If std::remove_all_extents_t<std::remove_reference_t<T>> is incomplete, then ranges::data(std::forward<T>(t)) is ill-formed, no diagnostic required.
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.
Expression-equivalent
Expression e is expression-equivalent to expression f, if e and f have the same effects, either are both potentially-throwing or are both not potentially-throwing (i.e. noexcept(e) == noexcept(f)), and either are both constant subexpressions or are both not constant subexpressions.
Customization point objects
The name ranges::data
denotes a customization point object, which is a const function object of a literal semiregular class type (denoted, for exposition purposes, as data_ftor
). All instances of data_ftor
are equal. Thus, ranges::data
can be copied freely and its copies can be used interchangeably.
Given a set of types Args...
, if std::declval<Args>()... meet the requirements for arguments to ranges::data
above, data_ftor
will satisfy std::invocable<const data_ftor&, Args...>. Otherwise, no function call operator of data_ftor
participates in overload resolution.
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.
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:
Hello world!
See also
(C++20) |
obtains a pointer to the beginning of a read-only contiguous range (customization point object) |
(C++20) |
returns an iterator to the beginning of a range (customization point object) |
(C++17) |
obtains the pointer to the underlying array (function template) |