Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::basic_ispanstream<CharT,Traits>::span

      From cppreference.com
      <cpp‎ |io‎ |basic ispanstream

      [edit template]
       
       
       
       
      std::span<const CharT> span()constnoexcept;
      (1)(since C++23)
      void span(std::span<CharT> s)noexcept;
      (2)(since C++23)
      template<class SpanLike>
      void span( SpanLike&& r)noexcept;
      (3)(since C++23)
      1) Gets aspan referencing the written area ifstd::ios_base::out is set in the open mode of the wrappedstd::basic_spanbuf, or aspan referencing the underlying buffer otherwise.
      2) Makes the wrappedstd::basic_spanbuf perform I/O on the buffer referenced bys.
      3) Same as(2), except thats is obtained as if by
      std::span<const CharT> cs{std::forward<SpanLike>(r)};
      std::span<CharT> s{const_cast<CharT*>(cs.data()), cs.size()};

      . This overload participates in overload resolution only ifSpanLike modelsborrowed_range,std::convertible_to<SpanLike,std::span<CharT>> isfalse, andstd::convertible_to<SpanLike,std::span<const CharT>> istrue.

      Contents

      [edit]Parameters

      s -std::span referencing the storage to be use as the new underlying buffer of stream
      r -borrowed_range to be use as the new underlying buffer of stream

      [edit]Return value

      1) Astd::span referencing the underlying buffer or written area, depending on the open mode of the wrappedstd::basic_spanbuf.
      2,3) (none)

      [edit]Example

      Run this code
      #include <cassert>#include <iostream>#include <span>#include <spanstream> int main(){char out_buf[16];std::ospanstream ost{std::span<char>{out_buf}};    ost<<"C++"<<' '<<23<<'\0';// note explicit null-terminationauto sp= ost.span();assert(        sp[0]=='C'&& sp[1]=='+'&& sp[2]=='+'&&        sp[3]==' '&& sp[4]=='2'&& sp[5]=='3'&&        sp[6]=='\0');std::cout<<"sp.data(): ["<< sp.data()<<"]\n";std::cout<<"out_buf: ["<< out_buf<<"]\n";// spanstream uses out_buf as internal storage, no allocationsassert(static_cast<char*>(out_buf)== sp.data()); constchar in_buf[]="X Y 42";std::ispanstream ist{std::span<constchar>{in_buf}};assert(static_cast<constchar*>(in_buf)== ist.span().data());char c;    ist>> c;assert(c=='X');    ist>> c;assert(c=='Y');int i;    ist>> i;assert(i==42);    ist>> i;// buffer is exhaustedassert(!ist);}

      Output:

      sp.data(): [C++ 23]out_buf: [C++ 23]

      [edit]See also

      obtains or initializes an underlying buffer according to mode
      (public member function ofstd::basic_spanbuf<CharT,Traits>)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/io/basic_ispanstream/span&oldid=130877"

      [8]ページ先頭

      ©2009-2025 Movatter.jp