Namespaces
Variants
Views
Actions

std::ranges::cdata

From cppreference.com
< cpp‎ | ranges
Revision as of 20:39, 3 July 2020 by Cjdb (Talk | contribs)

 
 
Ranges library
Range adaptors
 
Defined in header <ranges>
inline namespace /*unspecified*/ {

    inline constexpr /*unspecified*/ cdata = /*unspecified*/;

}
(1) (since C++20)
(customization point object)
Call signature
template< class T >

    requires /* see below */

constexpr std::remove_reference_t<ranges::range_reference_t<const T>>* cdata(T&& t);


Let t be an object of type T.

ranges::cdata is expression-equivalent to:

1) ranges::data(static_cast<const T&>(std::forward<T>(t))) if t is an lvalue.
2) ranges::data(static_cast<const T&&>(std::forward<T>(t))) if t is an rvalue.

If ranges::cdata(t) is valid, then it returns a pointer to a constant object.

Contents

Expression-equivalent

Expression e is expression-equivalent to expression f, if

  • e and f have the same effects, and
  • either both are constant subexpressions or else neither is a constant subexpression, and
  • either both are potentially-throwing or else neither is potentially-throwing (i.e. noexcept(e) == noexcept(f)).

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

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

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::cdata(s));
    //[s.data(), s.data() + s.size()] is guaranteed to be an NTBS since C++11
 
    std::cout << a;
}

Output:

Hello world!

See also

obtains a pointer to the beginning of a contiguous range
(customization point object)[edit]
(C++17)
obtains the pointer to the underlying array
(function template) [edit]