std::ranges::cdata(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

hey bestie!! you're on the std::ranges::cdata(3) page from the C++ Standard Library β€” the archive header spells it β€œC++ Standard Libary,” and we are leaving that little typo exactly where it is 🫢

🌸 NAME 🌸

std::ranges::cdata β€” literally the same name again, bestie: std::ranges::cdata. The manual entry is std::ranges::cdata(3).

✨ Synopsis ✨

It lives in two headers, no cap:

Defined in header <ranges>
Defined in header <iterator>

The object setup looks like this:

inline namespace /*unspecified*/ {
    inline constexpr /*unspecified*/ cdata = /*unspecified*/;
}

That's (since C++20), and it's a (customization point object) πŸ’…

Call signature

template< class T >
    requires /* see below */
constexpr /* see below */ cdata( T&& t );

Also (since C++20), bestie.

So what does it actually do? It 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 CT be:

  • const std::remove_reference_t<T>& if the argument is an lvalue (i.e. T is an lvalue reference type),
  • const T otherwise.

(until C++23)

A call to ranges::cdata is expression-equivalent to ranges::data(static_cast<CT&&>(t)).

The return type is equivalent to std::remove_reference_t<ranges::range_reference_t<CT>>*.

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

  • /*as-const-pointer*/(ranges::data(/*possibly-const-range*/(t))).

The return type is equivalent to std::remove_reference_t<ranges::range_const_reference_t<T>>*. (since C++23)

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

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. For exposition purposes, the cv-unqualified version of its type is denoted as __cdata_fn.

All instances of __cdata_fn are equal. The effects of invoking different instances of type __cdata_fn on the same arguments are equivalent, regardless of whether the expression denoting the instance is an lvalue or rvalue, and is const-qualified or not (however, a volatile-qualified instance is not required to be invocable). Thus, ranges::cdata 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::cdata above, __cdata_fn models:

  • std::invocable<__cdata_fn, Args...>,
  • std::invocable<const __cdata_fn, Args...>,
  • std::invocable<__cdata_fn&, Args...>, and
  • std::invocable<const __cdata_fn&, Args...>.

Otherwise, no function call operator of __cdata_fn participates in overload resolution. πŸ‘‘

πŸ’» Example πŸ’»

// Run this code

#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 πŸŽ€

Hello world!

πŸ‘€ See also πŸ‘€

  • ranges::data β€” obtains a pointer to the beginning of a contiguous range (C++20) (customization point object)
  • data β€” obtains the pointer to the underlying array (C++17) (function template)

made with 🫢 from the cppreference archive β€” source: http://cppreference.com, version/date: 2024.06.10. bye bestie, go be const-correct and iconic πŸ’…βœ¨

πŸ“¦ where this page comes from