Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::ranges::data

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

         inlineconstexpr/* unspecified */ data=/* unspecified */;

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

          requires/* see below */
      constexprstd::remove_reference_t<

         ranges::range_reference_t<T>>* data( T&& t);
      (since C++20)

      Returns a pointer to the first element of a contiguous range.

      IfT is an array type andstd::remove_all_extents_t<std::remove_reference_t<T>> is incomplete, then the call toranges::data is ill-formed, no diagnostic required.

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

      1. decay-copy(t.data())(until C++23)auto(t.data())(since C++23), if that expression is valid and its type is a pointer to an object type.
      2. Otherwise,std::to_address(ranges::begin(t)), if the expressionranges::begin(t) is valid and its type modelsstd::contiguous_iterator.

      In all other cases, a call toranges::data is ill-formed, which can result insubstitution failure whenranges::data(e) appears in the immediate context of a template instantiation.

      Contents

      Customization point objects

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

      [edit]Notes

      If the argument is an rvalue (i.e.T is an object type) andranges::enable_borrowed_range<std::remove_cv_t<T>> isfalse, the call toranges::data is ill-formed, which also results in substitution failure.

      Ifranges::data(e) is valid for an expressione, then it returns a pointer to an object.

      The C++20 standard requires that if the underlyingdata function call returns a prvalue, the return value is move-constructed from the materialized temporary object. All implementations directly return the prvalue instead. The requirement is corrected by the post-C++20 proposalP0849R8 to match the implementations.

      [edit]Example

      Run this code
      #include <cstring>#include <iostream>#include <ranges>#include <string> int main(){std::string s{"Hello world!\n"}; char a[20];// storage for a C-style stringstd::strcpy(a, std::ranges::data(s));// [data(s), data(s) + size(s)] is guaranteed to be an NTBS std::cout<< a;}

      Output:

      Hello world!

      [edit]See also

      obtains a pointer to the beginning of a read-only contiguous range
      (customization point object)[edit]
      returns an iterator to the beginning of a 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/data&oldid=160936"

      [8]ページ先頭

      ©2009-2025 Movatter.jp