std::ranges::cdata
Min standard notice:
Header: <ranges>
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.
# Declarations
inline namespace /*unspecified*/ {
inline constexpr /*unspecified*/ cdata = /*unspecified*/;
}
(since C++20) (customization point object)
Call signature
template< class T >
requires /* see below */
constexpr /* see below */ cdata( T&& t );
(since C++20)
# 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;
}