Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::span<T,Extent>::span

      From cppreference.com
      <cpp‎ |container‎ |span
       
       
       
       
      constexpr span()noexcept;
      (1)(since C++20)
      template<class It>

      explicit(extent!=std::dynamic_extent)

      constexpr span( It first, size_type count);
      (2)(since C++20)
      template<class It,class End>

      explicit(extent!=std::dynamic_extent)

      constexpr span( It first, End last);
      (3)(since C++20)
      template<std::size_t N>
      constexpr span(std::type_identity_t<element_type>(&arr)[N])noexcept;
      (4)(since C++20)
      template<class U,std::size_t N>
      constexpr span(std::array<U, N>& arr)noexcept;
      (5)(since C++20)
      template<class U,std::size_t N>
      constexpr span(conststd::array<U, N>& arr)noexcept;
      (6)(since C++20)
      template<class R>

      explicit(extent!=std::dynamic_extent)

      constexpr span( R&& r);
      (7)(since C++20)
      explicit(extent!=std::dynamic_extent)
      constexpr span(std::initializer_list<value_type> il)noexcept;
      (8)(since C++26)
      template<class U,std::size_t N>

      explicit(extent!=std::dynamic_extent&& N==std::dynamic_extent)

      constexpr span(conststd::span<U, N>& source)noexcept;
      (9)(since C++20)
      constexpr span(const span& other)noexcept=default;
      (10)(since C++20)

      Constructs aspan.

      Contents

      [edit]Parameters

      first - iterator to the first element of the sequence
      count - number of elements in the sequence
      last - iterator past the last element of the sequence or another sentinel
      arr - array to construct a view for
      r - range to construct a view for
      source - anotherspan to convert from
      other - anotherspan to copy from

      [edit]Effects

       Overload  data() after construction  size() after construction 
      (1)nullptr0
      (2)std::to_address(first)count
      (3)last- first
      (4)std::data(arr)N
      (5)
      (6)
      (7)ranges::data(r)ranges::size(r)
      (8)il.begin()il.size()
      (9)source.data()source.size()
      (10)other.data()other.size()

      [edit]Constraints and supplement information

      [edit]Size requirements

      Ifextent is notstd::dynamic_extent and the size of the source range is different fromextent, thespan object cannot be constructed.

      These overloads participate in overload resolution only if the result of the following expression istrue:

      1)extent==std::dynamic_extent|| extent==0
      4-6)extent==std::dynamic_extent|| extent== N
      9)extent==std::dynamic_extent|| N==std::dynamic_extent|| extent== N


      If the result of the following expression isfalse, the behavior is undefined.

      (until C++26)

      If the result of the following expression isfalse:

      • If the implementation ishardened, acontract violation occurs. Moreover, if the contract-violation handler returns under “observe” evaluation semantic, the behavior is undefined.
      • If the implementation is not hardened, the behavior is undefined.
      (since C++26)
      2)extent==std::dynamic_extent|| extent== count
      3)extent==std::dynamic_extent|| extent== last- first
      7)extent==std::dynamic_extent|| extent==ranges::size(r)
      8)extent==std::dynamic_extent|| extent== il.size()
      9)extent==std::dynamic_extent|| extent== source.size()

      [edit]Conversion requirements

      Ifelement_type is different from the element type of the source range, and the latter cannot be converted to the former byqualification conversion, thespan object cannot be constructed.

      These overloads participate in overload resolution only ifstd::is_convertible_v<U(*)[], element_type(*)[]> istrue, whereU is defined as follows:

      2,3)std::remove_reference_t<std::iter_reference_t<It>>
      4-6)std::remove_pointer_t<decltype(std::data(arr))>
      7)std::remove_reference_t<ranges::range_reference_t<R>>
      9)U

      [edit]Concept requirements

      If any template argument does not model certain concept(s), thespan object cannot be constructed.

      These overloads participate in overload resolution only if the template argument corresponding to the specified template parameter satisfies the corresponding concept(s). If it does not meet the semantic requirements of any corresponding concept, the behavior is undefined:

       Overload Template
       parameter 
      ConceptRemark
      (2)Itcontiguous_iterator
      (3)Itcontiguous_iterator
      End sized_sentinel_for<It> 
      (7)Rcontiguous_range
      sized_range
      borrowed_rangeonly required ifstd::is_const_v<element_type> isfalse

      [edit]Other constraints

      2) If[firstfirst+ count) is not avalid range, the behavior is undefined.
      3) This overload participates in overload resolution only ifstd::is_convertible_v<End,std::size_t> isfalse.
      If[firstlast) is not a valid range, the behavior is undefined.
      7) This overload participates in overload resolution only if all following conditions are satisfied:
      8) This overload participates in overload resolution only ifstd::is_const_v<element_type> istrue.

      [edit]Exceptions

      2) Throws nothing.
      3) Throws what and whenlast- first throws.
      7) Throws what and whenstd::ranges::size(r) andstd::ranges::data(r) throw.

      [edit]Notes

      Feature-test macroValueStdFeature
      __cpp_lib_span_initializer_list202311L(C++26)Constructingstd::span from astd::initializer_list,(8)

      [edit]Example

      Run this code
      #include <array>#include <iostream>#include <span>#include <vector> void print_span(std::span<constint> s){for(int n: s)std::cout<< n<<' ';std::cout<<'\n';} int main(){int c[]{1,2,3};    print_span(c);// constructs from array std::array a{4,5,6};    print_span(a);// constructs from std::array std::vector v{7,8,9};    print_span(v);// constructs from std::vector #if __cpp_lib_span_initializer_list    print_span({0,1,2});// constructs from initializer_list#else    print_span({{0,1,2}});// ditto, a workaround#endif}

      Output:

      1 2 3 4 5 67 8 90 1 2

      [edit]See also

      direct access to the underlying contiguous storage
      (public member function)[edit]
      returns the number of elements
      (public member function)[edit]
      assigns aspan
      (public member function)[edit]
      (C++17)(C++20)
      returns the size of a container or array
      (function template)[edit]
      (C++17)
      obtains the pointer to the underlying array
      (function template)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/container/span/span&oldid=182572"

      [8]ページ先頭

      ©2009-2025 Movatter.jp