Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::ranges::cdata

      From cppreference.com
      <cpp‎ |ranges
       
       
      Ranges library
      Range adaptors
       
      Defined in header<ranges>
      Defined in header<iterator>
      inlinenamespace/*unspecified*/{

         inlineconstexpr/*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)

      Returns a pointer to the first elementof constant type(since C++23) of a contiguous range denoted by aconst-qualified(until C++23) argument.

      LetCT be

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

      A call toranges::cdata isexpression-equivalent toranges::data(static_cast<CT&&>(t)).

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

      (until C++23)

      If the argument is an lvalue orranges::enable_borrowed_range<std::remove_cv_t<T>> istrue, then a call toranges::cdata isexpression-equivalent to:

      The return type is equivalent tostd::remove_reference_t<ranges::range_const_reference_t<T>>*.

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

      (since C++23)

      Ifranges::cdata(t) is valid, then it returns a pointer to an objectof constant type(since C++23).

      Customization point objects

      The nameranges::cdata denotes acustomization point object, which is a constfunction object of aliteralsemiregular class type. SeeCustomizationPointObject for details.

      [edit]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 stringstd::strcpy(dst, std::ranges::cdata(src));// [data(src), data(src) + size(src)] is guaranteed to be an NTBS std::cout<< dst;}

      Output:

      Hello world!

      [edit]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]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/ranges/cdata&oldid=179363"

      [8]ページ先頭

      ©2009-2025 Movatter.jp