std::ranges::cdata - cppreference.com
From cppreference.com
| Defined in header |
||
| Defined in header |
||
|
|
(since C++20) (customization point object) |
|
| Call signature |
||
|
|
(since C++20) | |
Returns a pointer to the first element of constant type(since C++23) of a contiguous range denoted by a const-qualified(until C++23) argument.
|
Let
A call to The return type is equivalent to |
(until C++23) |
|
If the argument is an lvalue or
The return type is equivalent to In all other cases, a call to |
(since C++23) |
If ranges::cdata(t) is valid, then it returns a pointer to an object of constant type(since C++23).
Customization point objects
The name ranges::cdata denotes a customization point object, which is a const function object of a literal semiregular class type. See CustomizationPointObject for details.
Example
#include <cstring> #include <iostream> #include <ranges> #include <string> int main() { std::string src {"hello world!\n"}; // std::ranges::cdata(src)[0] = 'H'; // error, src.data() is treated as read-only std::ranges::data(src)[0] = 'H'; // OK, src.data() is a non-const storage char dst[20]; // storage for a C-style string std::strcpy(dst, std::ranges::cdata(src)); // [data(src), data(src) + size(src)] is guaranteed to be an NTBS std::cout << dst; }
Output:
See also
| obtains a pointer to the beginning of a contiguous range (customization point object)[edit] | |
| obtains the pointer to the underlying array (function template) [edit] |