|
Range primitives | |||||||
|
Range concepts | |||||||||||||||||||
|
Range factories | |||||||||
|
Range adaptors | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
Helper items | |||||||||||||||||
|
Defined in header <ranges> | ||
Defined in header <iterator> | ||
inlinenamespace/* unspecified */{ inlineconstexpr/* unspecified */ data=/* unspecified */; | (since C++20) (customization point object) | |
Call signature | ||
template<class T> requires/* see below */ | (since C++20) | |
Returns a pointer to the first element of a contiguous range.
IfT
is an array type andstd::remove_all_extents_t<std::remove_reference_t<T>> is incomplete, then the call toranges::data
is ill-formed, no diagnostic required.
If the argument is an lvalue orranges::enable_borrowed_range<std::remove_cv_t<T>> istrue, a call toranges::data
isexpression-equivalent to:
In all other cases, a call toranges::data
is ill-formed, which can result insubstitution failure whenranges::data(e) appears in the immediate context of a template instantiation.
Contents |
The nameranges::data
denotes acustomization point object, which is a constfunction object of aliteralsemiregular
class type. SeeCustomizationPointObject for details.
If the argument is an rvalue (i.e.T
is an object type) andranges::enable_borrowed_range<std::remove_cv_t<T>> isfalse, the call toranges::data
is ill-formed, which also results in substitution failure.
Ifranges::data(e) is valid for an expressione, then it returns a pointer to an object.
The C++20 standard requires that if the underlyingdata
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 proposalP0849R8 to match the implementations.
#include <cstring>#include <iostream>#include <ranges>#include <string> int main(){std::string s{"Hello world!\n"}; char a[20];// storage for a C-style stringstd::strcpy(a, std::ranges::data(s));// [data(s), data(s) + size(s)] is guaranteed to be an NTBS std::cout<< a;}
Output:
Hello world!
(C++20) | obtains a pointer to the beginning of a read-only contiguous range (customization point object)[edit] |
(C++20) | returns an iterator to the beginning of a range (customization point object)[edit] |
(C++17) | obtains the pointer to the underlying array (function template)[edit] |